这是我创建对话框的代码
public void onClick(View v)
{
try{
Builder dialog= new AlertDialog.Builder(context);
dialog.setTitle(R.string.dialog_title1);
dialog.setMessage(R.string.url);
dialog.setPositiveButton(R.string.dialog_ok, null);
dialog.show();
}
我想为SetPositive按钮添加一个事件监听器(OK按钮)。单击确定时应关闭我的应用程序,即用户应退出应用程序。任何人都可以帮我实现这个目标吗?
答案 0 :(得分:17)
dialog.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
});
答案 1 :(得分:2)
试试这个:
new AlertDialog.Builder(this)
.setTitle("XYZ")
.setMessage("Do you want to Exit ?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
MyActivityClass.this.finish();
dialog.dismiss();
}
})
.setNeutralButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
})
.create()
.show();
答案 2 :(得分:0)
试用此代码
Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Message");
dialog.setMessage("Exit App?");
dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Enter your Code for exit Application
}
});
dialog.show();
答案 3 :(得分:0)
AlertDialog ad1=new AlertDialog.Builder(Hangman.this).create();
ad1.setMessage("You want to Exit");
ad1.setCancelable(false);
ad1.setButton("Exit", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
HomeActivity.this.finish();
}
});
试试这个.. :) 更新