[英]Dismiss AlertDialog with EditText android
我有一個具有EditText的AlertDialog。 此EditText接受用戶輸入,並在“發布”按鈕上單擊,以啟動API調用。
問題是,單擊“發布”按鈕后,AlertDialog應該立即關閉。 目前,我必須單擊外部區域以將其關閉。
如果我刪除了API調用,則單擊按鈕后,AlertDialog將正確關閉。
我不確定是怎么了。
這是我的代碼:
case R.id.btnAddComms:
scrollNews.fullScroll(v.FOCUS_DOWN);
btnAddComms.setPressed(true);
AlertDialog.Builder builder = new AlertDialog.Builder(NDetails.this);
builder.setTitle("Post Comment");
builder.setIcon(R.drawable.post_comment_button);
final EditText input1 = new EditText(NDetails.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input1.setLayoutParams(lp);
builder.setView(input1);
builder.setPositiveButton("Post", new DialogInterface.OnClickListener()
{
//private ITextInputDialogCalback callback;
public void onClick(DialogInterface dialog, int id)
{
postedComment = input1.getText().toString();
if(postedComment.length()>0)
{
dialog.cancel();
PostComments(postedComment);
}
else
{
Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
input1.findFocus();
}
}
private void PostComments(String postedComment)
{
// TODO Auto-generated method stub
String postCommentUrl = url;
try
{
String commentResponse = new PostComment().execute(postCommentUrl).get();
String getRequestForComments = myurl;
String items = new FetchItems().execute(getRequestForComments).get();
ArrayList<HashMap<String, String>> updatedCommentList = new GetList().execute(items).get();
itemsAdapter = (ListAdapter) new CommentsAdapter(NDetails.this, updatedCommentList);
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();
}
}
})
.setCancelable(false);
AlertDialog alert = builder.create();
alert.setCanceledOnTouchOutside(true);
alert.show();
break;
代替這個
AlertDialog alert = builder.create();
alert.setCanceledOnTouchOutside(true);
alert.show();
break;
采用
builder.create().show();
break;
做下一個:
AlertDialog alert; // Define it in your activity or fragment
...
case R.id.btnAddComms:
...
builder.setPositiveButton("Post", new DialogInterface.OnClickListener()
{
//private ITextInputDialogCalback callback;
public void onClick(DialogInterface dialog, int id)
{
postedComment = input1.getText().toString();
if(postedComment.length()>0)
{
alert.dismiss(); // Dismiss dialog here
PostComments(postedComment);
}
else
{
Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
input1.findFocus();
}
...
alert = builder.create();
alert.setCanceledOnTouchOutside(true);
alert.show();
break;
只需將對話框的“發布”按鈕單擊代碼替換為以下代碼:
更新:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("your message");
builder.setPositiveButton("Post", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
postedComment = input1.getText().toString();
if(postedComment.length()>0)
{
PostComments(postedComment);
dialog.dismiss();
}
else
{
Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
input1.findFocus();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
builder.show();
嘗試使用其他方式創建對話框。 可以使用類似這樣的東西:
new AlertDialog.Builder(ACT_NewsListings.this)
.setTitle("Title")
.setMessage("Message")
.setCancelable(false)
.setPositiveButton("Post", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
//YOUR STUFF HERE
}
})
.show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.