AlertDialog自动关闭

时间:2013-04-03 11:09:21

标签: java android filter android-alertdialog

我有一个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();

任何建议表示赞赏.. 感谢

1 个答案:

答案 0 :(得分:1)

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