首页
学习
活动
专区
工具
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
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18352324

复制
相关文章

相似问题

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