0

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

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

4

1 回答 1

1

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

于 2013-04-03T11:18:12.813 回答