java - AlertDialog 自动关闭

标签 java android filter android-alertdialog

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

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

现在,字段 (EditTexts) 已分别附加到它们。

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

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

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

如有任何建议,我们将不胜感激。 谢谢

最佳答案

为您的EditText添加addTextChangedListener,然后始终检查用户是否输入了任何文本,就好像没有禁用提交按钮一样动态启用提交按钮。

关于java - AlertDialog 自动关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15785690/

相关文章:

具有两个变量的Java递归

JAVA - 实例化 Scala 案例类

Java 编码 UTF8 的问题

android - 是否可以在模拟器上使用已发布的即时应用程序(BuzzFeed、Wish 等)?

java - Error Method太复杂,数据流算法无法分析

grails - 在 Grails 2 Web 应用程序过滤器中定义多个不同的 URI

javascript - 使用 fetch 时将 javascript 对象过滤到数组中

java - 我在 HashMap 中有 4 个项目并希望将它们存储在数组中

css - 固定位置不适用于 CSS 模糊过滤器

android根据切换按钮的状态验证textview