Android 使用 DialogInterface 的 onClick() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20494542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using DialogInterface's onClick() method
提问by Ved
Here is my code to initialize the AlertDialog
; buildexit
is AlertDialog.Builder
and exitalert
is AlertDialog
:
这是我的初始化代码AlertDialog
;buildexit
是AlertDialog.Builder
并且exitalert
是AlertDialog
:
buildexit=new AlertDialog.Builder(this);
buildexit.setTitle("Exit?");
buildexit.setIcon(R.drawable.ic_action_warning);
buildexit.setMessage("Do you want to really exit?");
buildexit.setPositiveButton("Yes", this);
buildexit.setNegativeButton("No", this);
exitalert=buildexit.create();
I want to terminate the application using Activity.finish()
method when the user clicks the "Yes" button, if "No" is click nothing should happen, I have also implemented the android.content.DialogInterface.OnClickListener
interface, the method implemented is public void onClick(DialogInterface arg0, int arg1)
.
Thanks in advance.
我想Activity.finish()
在用户单击“是”按钮时使用方法终止应用程序,如果“否”是单击什么都不应该发生,我也实现了android.content.DialogInterface.OnClickListener
接口,实现的方法是public void onClick(DialogInterface arg0, int arg1)
.
提前致谢。
回答by Kyrremann
I know this is an old question, but if you are like me, and ended here because you where googling, this is probably what you are looking for.
我知道这是一个古老的问题,但如果你像我一样,并且因为你在谷歌上搜索而到此结束,这可能就是你正在寻找的。
To avoid creating a new DialogInterface.onClickListener()
for each button, you can let the class implement AlertDialog.OnClickListener
, and then override public void onClick(DialogInterface dialog, int which)
.
为了避免DialogInterface.onClickListener()
为每个按钮创建一个新的,您可以让类实现AlertDialog.OnClickListener
,然后覆盖public void onClick(DialogInterface dialog, int which)
。
The int which
in this method is the button that was clicked, or the position. See the Android Referencefor more detailed information.
在int which
该方法中是被点击的按钮或位置。有关更多详细信息,请参阅Android 参考。
So if you want to check which button was clicked you can use a switch-case and look for the constants BUTTON_NEGATIVE
, BUTTON_NEUTRAL
, and BUTTON_POSITIVE
.
所以,如果你要检查哪个按钮被点击,您可以使用一个开关的情况下,寻找常数BUTTON_NEGATIVE
,BUTTON_NEUTRAL
和BUTTON_POSITIVE
。
Example:
例子:
// skipping package and imports
public class TestActivity extends Activity implements AlertDialog.OnClickListener {
void createAndShowDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.title);
builder.setMessage(R.string.message);
builder.setPositiveButton(android.R.string.yes, this);
builder.setNeutralButton(android.R.string.cancel, this);
builder.setNegativeButton(android.R.string.no, this);
builder.create().show();
}
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case BUTTON_NEGATIVE:
// int which = -2
dialog.dismiss();
break;
case BUTTON_NEUTRAL:
// int which = -3
dialog.dismiss();
break;
case BUTTON_POSITIVE:
// int which = -1
TestActivity.this.finish();
dialog.dismiss();
break;
}
}
回答by Rethinavel
Try this out :
试试这个:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
回答by Kirit Vaghela
AlertDialog buildexit = new AlertDialog.Builder(this)
.setTitle("Exit?")
.setIcon(R.drawable.ic_launcher)
.setMessage("Do you want to really exit?")
.setPositiveButton("Yes", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
}
})
.setNegativeButton("No", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
buildexit.show();