Android 仅当已填充时才使用EditText关闭AlertDialog
我在AlertDialog中有一个EditText。我只想在EditText中有一些输入时才取消它,否则会显示一个Toast提示用户输入数据 当前,当编辑文本包含数据时,对话框将正确关闭。当编辑文本为空时,也会显示toast。但是,即使EditText为空并且Toast消息稍后显示,对话框仍然会关闭 我不希望ALertDialog关闭,而EditText应该在显示Toast后再次获得焦点 这是我的密码:-Android 仅当已填充时才使用EditText关闭AlertDialog,android,android-edittext,android-alertdialog,dismiss,Android,Android Edittext,Android Alertdialog,Dismiss,我在AlertDialog中有一个EditText。我只想在EditText中有一些输入时才取消它,否则会显示一个Toast提示用户输入数据 当前,当编辑文本包含数据时,对话框将正确关闭。当编辑文本为空时,也会显示toast。但是,即使EditText为空并且Toast消息稍后显示,对话框仍然会关闭 我不希望ALertDialog关闭,而EditText应该在显示Toast后再次获得焦点 这是我的密码:- builder.setPositiveButton("Post", new DialogI
builder.setPositiveButton("Post", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
boolean worked = true;
postedComment = input1.getText().toString();
if(postedComment.length()==0 || postedComment=="" || postedComment.matches(""))
{
Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
input1.findFocus();
worked = false;
}
else if(worked && postedComment!="")
{
dialog.dismiss();
pd = new ProgressDialog(NewsDetails.this);
pd.setMessage("Posting..");
pd.show();
pd.setCancelable(true);
PostComments(postedComment);
}
})
.setCancelable(false);
alert = builder.create();
alert.setCanceledOnTouchOutside(true);
alert.show();
您需要为您的需求编写自定义对话框。请参见您需要使用自定义对话框而不是警报。因为您在单击按钮后没有控制权。这是在单击按钮之后,默认情况下它将关闭。@Biraj您能告诉我一个可能符合我的目的的示例吗?另外,您应该使用equals方法来比较字符串,而不是像这样:postedComment==。在您的情况下,TextUtils.isEmpty方法也会有用。请参阅下面的链接可能会对您有所帮助