Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按下后退键时的对话框_Java_Android_Android Alertdialog - Fatal编程技术网

Java 按下后退键时的对话框

Java 按下后退键时的对话框,java,android,android-alertdialog,Java,Android,Android Alertdialog,当我按下我的设备对话框的backkey时,我有一个游戏的alertDialog。如果我按“退出”,我的游戏结束,如果我按“取消”,则土司会出现3秒钟,3秒钟后我的游戏再次开始。 现在的问题是,当我第一次按下设备的backkey时,会出现一个对话框,我执行一些功能,它工作正常,但是如果我再次按下backkey而没有“取消”或“退出”游戏,那么我的游戏不会再次恢复,直到我按下“否”按钮 我想要的是,如果我按下backkey,然后出现对话框,如果我再次按下backkey,我的游戏将再次恢复,而不会再次

当我按下我的设备对话框的
backkey
时,我有一个游戏的
alertDialog
。如果我按“退出”,我的游戏结束,如果我按“取消”,则土司会出现3秒钟,3秒钟后我的游戏再次开始。 现在的问题是,当我第一次按下设备的
backkey
时,会出现一个对话框,我执行一些功能,它工作正常,但是如果我再次按下
backkey
而没有“取消”或“退出”游戏,那么我的游戏不会再次恢复,直到我按下“否”按钮

我想要的是,如果我按下
backkey
,然后出现对话框,如果我再次按下
backkey
,我的游戏将再次恢复,而不会再次显示对话框,谢谢

以下是我的警报对话框代码:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

         //  GamePanel.thread.setStoped(true);
            GamePanel.thread.setRunning(false);

// in the next line of code we also style the dialog through xml which i put in styles


            AlertDialog alertDialog = new AlertDialog.Builder(this,R.style.myBackgroundStyle).create();
            alertDialog.setTitle("Exit Alert");
            alertDialog.setMessage("Do you really want to exit the Game?");
            alertDialog.setButton("Quit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //Best way is firstly use finish() and after that use System.exit(0) to clear static variables. It will give you some free space.
                    // A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory
                    finish();
                    System.exit(0);
                    return;

                }
            });
            alertDialog.setButton2("Cancle", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            // dialog.cancel();
                            // GamePanel.thread.resume();
                            dialog.dismiss();
                            // When user press the "Cancle" button then game resume for 3 seconds then start again
                            // Here is the Code of the toasts and each toast appear with delay of one second.

                            toast = new Toast(Game.this);
                            TextView textView=new TextView(Game.this);
                            textView.setTextColor(Color.DKGRAY);
                            textView.setBackgroundColor(Color.TRANSPARENT);
                            textView.setTextSize(60);
                            textView.setText("READY!");
                            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

                            toast.setView(textView);
                            toast.show();

                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    // show toast 2.
                                    toast = new Toast(Game.this);
                                    TextView textView = new TextView(Game.this);
                                    textView.setTextColor(Color.DKGRAY);
                                    textView.setBackgroundColor(Color.TRANSPARENT);
                                    textView.setTextSize(140);
                                    textView.setText("3");
                                    // textView.setText("done!");
                                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

                                    toast.setView(textView);
                                    toast.show();
                                }
                            }, 2500);


                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    // show toast 2.
                                    toast = new Toast(Game.this);
                                    TextView textView = new TextView(Game.this);
                                    textView.setTextColor(Color.DKGRAY);
                                    textView.setBackgroundColor(Color.TRANSPARENT);
                                    textView.setTextSize(140);
                                    textView.setText("2");
                                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

                                    toast.setView(textView);
                                    toast.show();
                                }
                            }, 5000);

                            new Handler().postDelayed(new Runnable() {
                                @Override public void run() {
                                    toast = new Toast(Game.this);
                                    TextView textView=new TextView(Game.this);
                                    textView.setTextColor(Color.DKGRAY);
                                    textView.setBackgroundColor(Color.TRANSPARENT);
                                    textView.setTextSize(140);
                                    textView.setText("1");
                                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

                                    toast.setView(textView);
                                    toast.show();

                                }
                            }, 7500);

                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run()
                                    GamePanel.thread.setRunning(true);
                                }
                            }, 10000);


                            return;
                        }
                    }

            );
            alertDialog.show();

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}

您需要在对话框中设置一个
OnKeyListener
,并检查是否按下了后退键

下面是一个适合您需要的示例代码,您需要按如下方式修改代码:

AlertDialog alertDialog = new AlertDialog.Builder(this,R.style.myBackgroundStyle).create();
        alertDialog.setTitle("Exit Alert");
        alertDialog.setMessage("Do you really want to exit the Game?");
        alertDialog.setButton("Quit", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //Best way is firstly use finish() and after that use System.exit(0) to clear static variables. It will give you some free space.
                // A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory
                finish();
                System.exit(0);
                return;

            }
        });


//New part regarding the back key when only dialog is shown.
alertDialog.setOnKeyListener(new Dialog.OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface arg0, int keyCode,
                KeyEvent event) {

            if (keyCode == KeyEvent.KEYCODE_BACK) {

                alertDialog.dismiss();
            }
            return true;
        }
    });

这只有在显示对话框时才起作用,因此,您可以返回到您的活动

您能告诉我需要将此代码放在哪里吗?请通过添加您的代码来编辑我的代码。感谢实现这一点还有一种更简单的方法:使用
alertDialog.setCancelable(true)如果您不希望在用户触摸对话框外部时将其取消,请同时使用
alertDialog.setCanceledOnTouchOut(false)