用户输入android后用edittext关闭警报对话框
我有一个AlertDialog,它有一个EditText来接受用户输入。它有一个“Post”按钮,通过web服务发布用户输入。我希望在用户单击“发布”按钮后立即关闭对话框。当前,仅当我在对话框外部单击时,对话框才会关闭 这是我的密码:-用户输入android后用edittext关闭警报对话框,android,android-alertdialog,dismiss,Android,Android Alertdialog,Dismiss,我有一个AlertDialog,它有一个EditText来接受用户输入。它有一个“Post”按钮,通过web服务发布用户输入。我希望在用户单击“发布”按钮后立即关闭对话框。当前,仅当我在对话框外部单击时,对话框才会关闭 这是我的密码:- AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Post comment"); fina
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Post comment");
final EditText input1 = new EditText(this);
builder.setView(input1);
builder.setPositiveButton("Post", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
postedComment = input1.getText().toString();
if(postedComment == null || postedComment=="")
{
Toast.makeText(getApplicationContext(), "Please enter your comment", Toast.LENGTH_SHORT).show();
}
else
{
dialog.dismiss();
String postCommentUrl = EndPoints.PostCommentsURL;
try
{
String commentResponse = new PostComment().execute(postCommentUrl).get();
String getRequestForComments = EndPoints.GetCommentsUrl+"?params;
String items = new FetchItems().execute(getRequestForComments).get();
ArrayList<HashMap<String, String>> updatedList = new GetList().execute(items).get();
itemsAdapter = (ListAdapter) new CommentsAdapter(Details.this, updatedList);
commentsList.invalidate();
commentsList.refreshDrawableState();
commentsList.setAdapter(itemsAdapter);
commentsList.post(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
commentsList.setSelection(itemsAdapter.getCount()-1);
}
});
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ExecutionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
AlertDialog alert = builder.create();
alert.setCanceledOnTouchOutside(false);
alert.show();
AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setMessage(“Post comment”);
最终编辑文本输入1=新编辑文本(本);
builder.setView(输入1);
setPositiveButton(“Post”,新的DialogInterface.OnClickListener()对话框)
{
public void onClick(DialogInterface对话框,int-id)
{
postedComment=input1.getText().toString();
如果(postedComment==null | | postedComment==“”)
{
Toast.makeText(getApplicationContext(),“请输入您的评论”,Toast.LENGTH\u SHORT.show();
}
其他的
{
dialog.dismise();
字符串postCommentUrl=EndPoints.PostCommentsURL;
尝试
{
String commentResponse=new PostComment().execute(postCommentUrl.get();
字符串getRequestForComments=EndPoints.GetCommentsUrl+“?参数;
String items=new FetchItems().execute(getRequestForComments.get();
ArrayList updatedList=新建GetList().execute(items).get();
itemsAdapter=(ListAdapter)新的CommentsAdapter(Details.this,updatedList);
commentsList.invalidate();
commentsList.refreshDrawableState();
commentsList.setAdapter(itemsAdapter);
commentsList.post(新的Runnable()
{
@凌驾
公开募捐
{
//TODO自动生成的方法存根
commentsList.setSelection(itemsAdapter.getCount()-1);
}
});
}
捕捉(中断异常e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
捕获(执行例外)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
});
AlertDialog alert=builder.create();
alert.setCanceledOnTouchOutside(假);
alert.show();
试试这个
如果条件
改变这一点:
if(postedComment == null || postedComment=="")
到
字符串应与.equals()
进行比较,因为==
仅比较两个引用
编辑
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
builder2.setTitle("Enter an item:");
final EditText input2 = new EditText(this);
builder2.setView(input2);
builder2.setPositiveButton("Add",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.v("asasas", "" + input2.getText().toString());
dialog.dismiss();
input2.setText("");
Intent intent = new Intent(MainActivity.this,
NextActivity.class);
startActivity(intent);
}
});
builder2.setNegativeButton("Stop",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//dialog.cancel();
dialog.dismiss();
}
});
builder2.show();
请尝试以下代码:
public static void showInputTextDialog(final Context context, String title, String message, final ITextInputDialogCalback callback)
{
if(!NetworkUtility.isOnline(context))
{
Toast.makeText(context, "Please check your network connectivity.", Toast.LENGTH_LONG).show();
return;
}
LayoutInflater layoutInflater = LayoutInflater.from(context);
View promptView = layoutInflater.inflate(R.layout.text_input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle(title);
alertDialogBuilder.setView(promptView);
((TextView) promptView.findViewById(R.id.textViewTitle)).setText(message);
final EditText input = (EditText) promptView.findViewById(R.id.userInput);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
if(input.getText().toString().length()>0)
{
callback.onClose(input.getText().toString(), true);
dialog.cancel();
}
else
{
Toast.makeText(context, "Please enter query.", Toast.LENGTH_LONG).show();
input.findFocus();
}
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
callback.onClose(input.getText().toString(), false);
dialog.cancel();
}
});
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
}
回调接口:
public interface ITextInputDialogCalback
{
void onClose(String inputValue, boolean isSuccess);
}
在回调接口中,您将得到回调,这样您就可以使用输入文本执行任何操作。您需要使用此选项
if(postedComment.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your comment", Toast.LENGTH_SHORT).show();
}
else {
// Execute Post function here.
dialog.dismiss();
}
单击按钮时会发生什么情况?@Hariharan On button click会调用web服务。但是对话框不会被取消。您的对话框名称是什么?您的对话框名称。取消();添加对话框。取消()如果之前的条件也没有帮助,同样resulkt@user2625086现在我试过了。它工作正常。你能参考我的代码发布吗?“dialog”是
onClick中的DialogInterface
对象(DialogInterface dialog,int-id)
@user2625086查看我的编辑。从对话框界面
中,只有您可以关闭或取消您的对话框。当我单击“发布”按钮时,对话框将再次创建。单击MethodosseCanceledOnTouchOut时,Dismise()已位于“发布”按钮中(false)添加此方法以防止在对话框窗口单击外部关闭对话框。我这样做了。主要问题不是在单击外部后关闭对话框。我希望在用户输入输入并单击ITextInputDialogCalback界面vcontain的post按钮后关闭对话框
if(postedComment.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your comment", Toast.LENGTH_SHORT).show();
}
else {
// Execute Post function here.
dialog.dismiss();
}