大家好,我的申请有问题 请参阅下面的代码以供引用。 我面临的问题是,当我从我的模拟器中单击返回键按钮时,我希望我的应用程序询问我是否确定要退出。当我按是时,它应该退出,否则它不应该。 我已经完成了所有的事情,但是当我点击后退键按钮时,它会显示一个对话框并自动退出。这是什么原因
代码:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder alert_wifi = new AlertDialog.Builder(this);
alert_wifi.setMessage("This is a Wi-Fi Service. Your Device does not have an active Wi-Fi Connection. Do you Want to Activate Wi-Fi Connection")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
dialog.dismiss();
ViewCamera.isStop = true;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
loading_act.finish();
}
});
AlertDialog alert = alert_wifi.create();
// Title for AlertDialog
alert.setTitle("Caution!");
alert.setIcon(R.drawable.wifi);
// Icon for AlertDialog
alert.show();
}
return super.onKeyDown(keyCode, event);
}
可能是什么问题请帮忙
最佳答案
return super.onKeyDown(keyCode, event);
改变它return true;
调用 super 使 Activity 执行默认行为,因此它退出。
关于android - android 中的 onKeyDown 方法重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5244905/