可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have the following AlertDialog:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
dialogBuilder.setTitle(R.string.title);
dialogBuilder.setMessage(mContext.getString(R.string.message));
dialogBuilder.setPositiveButton(R.string.positive, new MyOnClickListener());
dialogBuilder.setNegativeButton(R.string.negative, new MyOnClickListener());
dialogBuilder.show();
with this ClickListener
public static class MyOnClickListener implements DialogInterface.OnClickListener{
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
I would expect the dialog to be closed, when clicking on either of the buttons, but the dialog stays open instead.
I debugged the onClick method and the line
dialog.dismiss()
is being executed, but nothing happens.
Where am I going wrong or how can I fix this?
回答1:
Declare your AlertDialog
at the top like:
private AlertDialog myAlertdialog;
than replace your dialogBuilder.show();
with
myAlertDialog = dialogBuilder.create();
myAlertDialog.show();
than u can call myAlertDialog.dismiss();
回答2:
Try this
AlertDialog alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to AndroidHive.info");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
回答3:
Trying below code while using interface
to create your own dialog button click listener.
MyOnClickListener.java
import android.content.DialogInterface;
public interface MyOnClickListener extends DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog, int which);
}
Your dialog Code:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
dialogBuilder.setTitle("You Title");
dialogBuilder.setMessage("Here Is your message");
dialogBuilder.setPositiveButton("YES", new MyOnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "YES Clicked", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
dialogBuilder.setNegativeButton("NO", new MyOnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "NO Clicked", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
dialogBuilder.show();
Output:
Open your dialog:
Performing OnClick of dialog:
I hope its helps you.
回答4:
try this ... it worked for me ... You can use this method for your dialog
or you can simply use alert.dismiss(); to dismiss the dialog .... or just put the dialog method in your code and call the method i.e. MyAlertDialogBox(); wherever you want a dialog box .
AlertDialog alert;
// Dialog method .
private void MyAlertDialogBox(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Welcome ! This is a Alert dialog")
.setCancelable(false)
//You can name you strings according to your preference here
//or the String.xml file
.setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener()
{
public void onClick(final DialogInterface dialog, int id) {
Toast.makeText(MyActivityName.this, "Toast message
when you click Yes ", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton( getResources().getString(R.string.no),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(MyActivityName.this, "Toast message
when you click No ", Toast.LENGTH_SHORT).show();
// To dismiss your alert dialog box on click of
No button (or you can say the negative button)
alert.dismiss();
}
});
//Creating dialog box
alert = builder.create();
/* To give dialog box title
alert.setTitle(getResources().getString(R.string.folder));*/
/* to set icon in alert dialog box
alert.setIcon(R.drawable.ic_create_new_folder_white_24dp);*/
alert.show();
}
回答5:
Instead of dialog.dismiss()
, use dialoginterface.dismiss()
.
I've shown an example below
public void onClick(DialogInterface dialogInterface, int i)
{
dialogInterface.dismiss();
}
回答6:
When you change the line dialogBuilder.show()
to dialogBuilder.create().show()
it should be working correctly.