android中的onKeyDown方法覆盖

Sha*_*hah 1 android

大家好我的应用程序有问题,请参阅下面的代码以供参考.我面临的问题是当我从我的模拟器中点击后退键按钮时,我希望我的应用程序问我你确定要退出.当我按是然后它应该退出否则它不应该退出.我做了所有的事情,但当我点击后退键按钮时,它会显示一个对话框并自动退出.什么原因

码:

@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);
}
Run Code Online (Sandbox Code Playgroud)

可能有什么问题请帮忙

Vla*_*nov 7

return super.onKeyDown(keyCode, event);
Run Code Online (Sandbox Code Playgroud)

更改 return true;

调用super使活动执行默认行为,因此它将退出.