【问题标题】:AlertDialog Closing AutomaticallyAlertDialog 自动关闭
【发布时间】:2013-04-03 11:09:20
【问题描述】:

我有一个AlertDialog 用于向用户显示一个小表单。

在 ALertDialog 上有 2 个按钮;即“提交”和“取消”。

现在字段 (EditTexts) 已单独附加 setKeyListeners。

我面临的问题是假设用户没有填写任何字段并直接点击Submit按钮然后对话框自动关闭。

这是用于创建/显示对话框的方法:

Context ctx = this.getApplicationContext();
LinearLayout layoutCreateMerch = new LinearLayout(ctx);
layoutCreateMerch.setOrientation(LinearLayout.VERTICAL);
layoutCreateMerch.setVerticalScrollBarEnabled(true);

final AlertDialog.Builder alert = new AlertDialog.Builder(Store.this);
alert.setTitle("New Store");
final EditText stoName = new EditText(Store.this);
final EditText stoDesc = new EditText(Store.this);


InputFilter[] FilterMaxLen = new InputFilter[1];
FilterMaxLen[0] = new InputFilter.LengthFilter(25);

stoName.setFilters(FilterMaxLen);
stoName.setHint("Store's Name");
stoName.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,'1234567890 ")); 
stoName.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
layoutCreateMerch.addView(stoName);

stoDesc.setFilters(FilterMaxLen);
stoDesc.setHint("Store's Description");
stoDesc.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,'1234567890 ")); 
stoDesc.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
layoutCreateMerch.addView(stoDesc);

ScrollView scroll = new ScrollView(ctx);
scroll.setBackgroundColor(Color.TRANSPARENT);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
scroll.addView(layoutCreateMerch);

alert.setView(scroll);

alert.setNeutralButton("Submit",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                if (Name.getText().toString().equals("") 
                        || Desc.getText().toString().equals(""))
                {

                    if(stoName.getText().toString().equals("")){
                        stoName.setHint("fill Store's Name");
                        stoName.setHintTextColor(Color.RED);
                    }
                    else{}
                    if( stoDesc.getText().toString().equals("")){
                        stoDesc.setHint("fill Store's Description");
                        stoDesc.setHintTextColor(Color.RED);
                    }
                    else{}
                    if..
                    ..
                    ..

                } 

                else {

                    System.out.println("should not exit :| ");
                }

            }
        });

alert.setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int whichButton) {

                dialog.cancel();
            }
        });
alert.show();

感谢任何建议.. 谢谢

【问题讨论】:

    标签: java android filter android-alertdialog


    【解决方案1】:

    为您的EditText 添加addTextChangedListener,然后始终检查用户是否输入了任何文本,就好像不是disable 提交按钮,否则enable 动态提交按钮。

    【讨论】:

    • 感谢您的回复。但这是关于过滤文本.. :|
    • 您到底在寻找什么??就像您单击警报对话框按钮一样,对话框将被解除其默认行为。这就是为什么我建议您在用户未输入任何内容时禁用该按钮。顺便说一句,它根本不是过滤文本。
    • 即使我将其设置为 .setNeutralButton ?我认为它应该尊重dialog.dismiss() 而不会自动关闭:|
    • 谁说我的意思是您发现的中立位置不会关闭对话框,如果您真的不想关闭对话框,可以将 setCancelable 标志设置为 false 以提醒对话框。
    • 将对话框的 setCancelable 标志设置为 false 也无济于事 :( 感谢您提醒我有关对话框的默认行为 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多