我正在尝试制作一个对话框按钮,当我在主活动上时,它会弹出,并询问用户是否确定要关闭程序。我不确定要在//Action中为Yes按钮添加什么内容...有没有更好的方法来做这件事?
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle("Exit Game?");
// Icon for AlertDialog
alert.setIcon(R.drawable.icon);
alert.show();
return true;
}
return super.onKeyDown(keyCode, event);
}发布于 2011-12-01 16:38:59
您可以在yes按钮的操作中使用简单的finish()语句退出活动
您也可以为此重写onBackPressed方法
https://stackoverflow.com/questions/8338418
复制相似问题