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

标签 android button layout dialog android-alertdialog

我有带有正面和负面按钮的 AlertDialog。在 AlertDialog 布局中,我有 EditText 和两个按钮(btnAdd1、btnAdd2)。我希望当用户单击按钮 btnAdd1 或 btnAdd2 时将相同的文本添加到 AlertDialog 中的 EditText(但不关闭 AlertDialog)。这可能在 AlertDialog 中执行,还是我必须只使用 Dialog?

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

<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>

这是源代码:

    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() 设置为这两个按钮。

最佳答案

以下代码将从 R.layout.prompt 中扩充 View 并将其设置为 AlertDialog。 positivenegative 按钮将不会被使用。您可以为 btnAdd1btnAdd2 设置 onClick 行为:

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();

关于android - 如何将自定义按钮添加到 AlertDialog 布局中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18352324/

相关文章:

android - 在 Android 相机上拍照,无需点击按钮或屏幕

android - 返回无效,getItemViewType 行为非常奇怪 - 我无法解释

java - 无法访问android接口(interface)

ios - Swift:使用 TableView 外部的按钮并检查选择了哪一行

magento - 从观察者覆盖整个magento布局

android - 我可以阅读其他语言并使用 TTS 说话吗,例如 "Bengali ","Hindi "

Android:更改后退按钮的操作

javascript - D3 - 让 child 填充 parent 组

android - 动态位置布局

单击时更改 JavaScript 按钮样式