【问题标题】:AlertDialog with LinearLayout should not dismiss on button click带有 LinearLayout 的 AlertDialog 不应在单击按钮时关闭
【发布时间】:2017-04-12 23:08:40
【问题描述】:

我只希望我的AlertDialog 在满足某些条件时被解雇(当给出的姓名和姓氏有效时) - 否则它应该始终位于父视图的顶部。我的代码是这样的:

    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    final TextView instructions = new TextView(this);
    instructions.setText(R.string.alert_enter_data);
    final EditText name = new EditText(this);
    name.setHint(R.string.name);
    final EditText surname = new EditText(this);
    surname.setHint(R.string.surname);
    LinearLayout ll=new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.addView(instructions);
    ll.addView(name);
    ll.addView(surname);
    alert.setView(ll);
    alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String name_txt = name.getText().toString();
            String surname_txt = surname.getText().toString();
            if ((name_txt.length() > 1) && (surname_txt.length() > 1)) {
                dialog.dismiss();
            }
        }
    });
    final AlertDialog alert_dialog = alert.create();
    alert_dialog.setCanceledOnTouchOutside(false);
    alert_dialog.show();

使用此代码,无论输入文本如何,按下按钮时AlertDialog 都会消失。然后我尝试了这个:

alert_dialog.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String name_txt = name.getText().toString();
                String surname_txt = surname.getText().toString();
                String email_txt = email.getText().toString();
                String cellphone_txt = cellphone.getText().toString();
                String postcode_txt = postcode.getText().toString();
                if ((name_txt.length() > 1) && (surname_txt.length() > 1) && (email_txt.length() > 4)) {
                    if (debug_mode) {Log.i(TAG,"clause 1");}
                    String data_to_upload = name_txt + ", " + surname_txt + ", " + email_txt + ", "+ cellphone_txt + ", " + postcode_txt + "\n";
                    // upload_to_github(data_to_upload);
                    alert_dialog.dismiss();
                }
            }
        });
    }
});

但是这样我根本就没有 Button。警报对话框仅包含 EditText 字段。

【问题讨论】:

  • 你读过这里吗?:stackoverflow.com/a/27345656/2910520。您需要使用dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(...) 覆盖按钮侦听器以防止AlertDialog 在选择单击时自行关闭。您可以删除 showListener 因为它不是必需的,请注意在更改按钮侦听器之前您需要 create()show()对话框
  • 这就是我在第二个代码片段中所做的,但这样我根本没有得到任何按钮 - 我想是因为我使用的是LinearLayout,但我在这里迷路了。

标签: android


【解决方案1】:
alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {

Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);

两者都是不同的按钮。

试试

按钮 b = alert.getButton(AlertDialog.BUTTON_NEUTRAL);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 2019-07-02
    • 2011-12-14
    • 1970-01-01
    相关资源
    最近更新 更多