首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将自定义按钮添加到AlertDialog的布局中?

如何将自定义按钮添加到AlertDialog的布局中?
EN

Stack Overflow用户
提问于 2013-08-21 16:22:16
回答 6查看 43K关注 0票数 26

我有一个带有正负按钮的AlertDialog。在AlertDialog布局中,我有EditText和两个按钮(btnAdd1,btnAdd2)。我想当用户点击按钮btnAdd1或btnAdd2时添加相同的文本到AlertDialog中的EditText (但没有关闭AlertDialog)。这是不是可以在AlertDialog中做,或者我只能使用对话框?

这是AlertDialog的布局(R.layout.prompt):

代码语言:javascript
复制
<LinearLayout>
<EditText
    android:id="@+id/userInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btnAdd1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="bla" />

<Button
    android:id="@+id/btnAdd2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="bla" />

  </LinearLayout>

这是源代码:

代码语言:javascript
复制
    LayoutInflater layoutInflater = LayoutInflater.from(this);
        View promptView = layoutInflater.inflate(R.layout.prompt, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setView(promptView);
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                              //...

                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

    AlertDialog alertD = alertDialogBuilder.create();
    alertD.show();

我想从布局中访问btnAdd1和btnAdd2。将OnClickListener()设置为这两个按钮。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-08-21 17:07:32

下面的代码将从R.layout.prompt扩展视图并将其设置为AlertDialog。将不使用positivenegative按钮。您可以为btnAdd1btnAdd2设置onClick行为

代码语言:javascript
复制
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.prompt, null);

final AlertDialog alertD = new AlertDialog.Builder(this).create();

EditText userInput = (EditText) promptView.findViewById(R.id.userInput);

Button btnAdd1 = (Button) promptView.findViewById(R.id.btnAdd1);

Button btnAdd2 = (Button) promptView.findViewById(R.id.btnAdd2);

btnAdd1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        // btnAdd1 has been clicked

    }
});

btnAdd2.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        // btnAdd2 has been clicked

    }
});

alertD.setView(promptView);

alertD.show();
票数 57
EN

Stack Overflow用户

发布于 2013-08-21 16:29:25

你想做的是;

代码语言:javascript
复制
alertD.show();
Button button = (Button)promptView.findViewById(R.id.buttonId);
button.setOnClickListener(....)

使用视图调用findViewById,而不是activity,后者将在正在显示的布局中查找id。

票数 4
EN

Stack Overflow用户

发布于 2015-08-22 14:49:08

根据这种方法,我能够创建图像按钮,但如果我想取消或取消对话框上的取消按钮,那么我必须做的。

代码语言:javascript
复制
public static void alertDialogShow(final Context context,
            final String resultMobile) {

        LayoutInflater li = LayoutInflater.from(context);
        View promptsView = li.inflate(R.layout.prompt,
                null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        // set prompts.xml to alertdialog builder
        alertDialogBuilder.setView(promptsView);
        final EditText userInput = (EditText) promptsView
                .findViewById(R.id.editTextDialogUserInput);
        userInput.setText(resultMobile);
        userInput.setEnabled(false);
btnCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

            }
        });
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18352324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档