gpt4 book ai didi

java - AlertDialog 自动关闭

转载 作者:行者123 更新时间:2023-12-01 14:42:41 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com