我有一个AlertDialog,它有一个EditText来接受用户输入。它有一个“发布”按钮,通过Web服务发布用户输入。我希望用户单击“发布”按钮后立即关闭对话框。目前,仅当我在对话框外单击时,对话框才会解除。
这是我的代码: -
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();
答案 0 :(得分:1)
试试这个..
在dialog.dismiss();
if condition
并改变这一点:
if(postedComment == null || postedComment=="")
到
if(postedComment == null || postedComment.equals(""))
字符串应与.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();
答案 1 :(得分:0)
尝试以下代码:
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);
}
在回调界面中,您将获得回调,以便您可以使用输入文本执行任何操作。
答案 2 :(得分:0)
你需要使用它。
if(postedComment.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your comment", Toast.LENGTH_SHORT).show();
}
else {
// Execute Post function here.
dialog.dismiss();
}