【问题标题】:how to add second or third EditText to an AlertDialog如何将第二个或第三个 EditText 添加到 AlertDialog
【发布时间】:2011-12-15 05:17:41
【问题描述】:

朋友们,

我很新,很抱歉这个基本问题,但经过几个小时的搜索,我放弃了。 如何向我的 AlertDialog 添加第二个 EditText?它只显示一个带有两个按钮的 Edittext。 第二个 EditText 根本不显示。

这是我的代码,

final AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
final EditText inputstreet = new EditText(ctx);
final EditText inputstreetnumber = new EditText(ctx);

alert.setView(inputstreet);
alert.setView(inputstreetnumber);
               alert.setTitle(getResources().getString(R.string.t_MainAlertEnterAdressTitle));
// alert.setIcon(R.drawable.huji2); // Icon disabled for now
alert.setMessage(getResources().getString(R.string.t_MainAlertEnterAdressMessage));
alert.setPositiveButton(getResources().getString(R.string.t_MainAlertEnterAdressButtonOk),
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog,
                    int whichButton) {


                finish();
            }
        });

alert.setNegativeButton(getResources().getString(R.string.t_MainAlertEnterAdressButtonBack),
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog,
                    int whichButton) {

                dialog.cancel();

            }
        });
alert.show();

我删除了所有不重要的内容。非常感谢!!!

【问题讨论】:

    标签: android android-edittext android-alertdialog


    【解决方案1】:

    您的警报对话框只能容纳一个视图,因此您必须将 EditText 视图放在一个布局视图中,如下所示:

    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    
    final LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    
    final EditText inputstreet = new EditText(this);
    final EditText inputstreetnumber = new EditText(this);
    
    layout.addView(inputstreet);
    layout.addView(inputstreetnumber);
    
    alert.setView(layout);
    

    【讨论】:

    • 效果惊人。非常感谢!!!您可能知道如何更改 EditText 键盘的输入类型?
    • 如果您希望用户能够输入数字,请查看here。否则,请发布一个新问题
    猜你喜欢
    • 2021-11-24
    • 2013-08-29
    • 2021-10-19
    • 2023-01-11
    • 1970-01-01
    • 2011-03-30
    • 2014-04-22
    • 1970-01-01
    • 2013-07-12
    相关资源
    最近更新 更多