0
我有一個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();
任何建議表示讚賞.. 感謝
感謝您的回覆。但是這是關於過濾文本..:| – beerBear
你在找什麼?就好像您單擊警報對話框按鈕一樣,對話框將被視爲默認行爲。這就是爲什麼我建議你禁用該按鈕,如果用戶沒有輸入任何東西。順便說一下,它根本就沒有過濾文本。 –
即使我將它設置爲'.setNeutralButton'?我認爲它應該尊重'dialog.dismiss()'而不是自動關閉:| – beerBear