web-dev-qa-db-ja.com

戻るボタンを押した後にダイアログボックスを表示する方法

戻るボタンをクリックして、TextViewとexitというボタンで構成されるダイアログボックスを表示します。終了ボタンをクリックした後、それは私のアプリから出てくるはずです

私はこれが好きでした、

@Override       
public void onBackPressed() {       
    System.out.println("hiiii");
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog);

    Button exitButton = (Button) dialog.findViewById(R.id.exit);
    System.out.println("inside dialog_started");
    exitButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            MainActivity.this.finish();
            dialog.dismiss();
        }
    });
    return;
}

log cat hiiiiiで「dialog_started内」が出力されますが、ダイアログボックスは表示されません。戻るボタンをクリックしたときにそのダイアログボックスを取得するにはどうすればよいですか?

17
Jyosna
 public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        exitByBackKey();

        //moveTaskToBack(false);

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

protected void exitByBackKey() {

    AlertDialog alertbox = new AlertDialog.Builder(this)
    .setMessage("Do you want to exit application?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        // do something when the button is clicked
        public void onClick(DialogInterface arg0, int arg1) {

            finish();
            //close();


        }
    })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {

        // do something when the button is clicked
        public void onClick(DialogInterface arg0, int arg1) {
                       }
    })
      .show();

}
31
Sandy

これはより簡単な解決策です:

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Save Or Not");
    builder.setMessage("Do you want to save this? ");
    builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            saveResult();
            MyActivity.super.onBackPressed();
        }
    });
    builder.setNegativeButton("Discard", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            MyActivity.super.onBackPressed();
        }
    });
    builder.show();
}
6
Yuchen Zhong

これを試して...

     public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK) {

         AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Exit Alert");
            alertDialog.setIcon(R.drawable.appicon);

            alertDialog.setMessage("Do you really want to exit the Game?");
            alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                  finish();
                return;
            } }); 
            alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
                return;
            }}); 
              alertDialog.show();

         return true;
     }
     return super.onKeyDown(keyCode, event);
 }
0
star angel
    @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode != KeyEvent.KEYCODE_BACK)  return super.onKeyDown(keyCode, event);

            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                case DialogInterface.BUTTON_POSITIVE:
                    //Yes button clicked
                   finish();
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    //No button clicked
                    break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
            .setNegativeButton("No", dialogClickListener).show();





        return super.onKeyDown(keyCode, event);
    }
0
Rasel

終了メッセージを表示する別のコードを次に示します。

public void onBackPressed() { 
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        Menu.this);

    // set title
    alertDialogBuilder.setTitle("Exit");

    // set dialog message
    alertDialogBuilder
        .setMessage("Do you really want to exit?")
        .setCancelable(false)
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, close
                // current activity
                Menu.this.finish();
            }
          })
        .setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, just close
                // the dialog box and do nothing
                dialog.cancel();
            }
        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }
0
Vamsi Tallapudi
its working exactly....




public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitByBackKey();

            //moveTaskToBack(false);

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

    protected void exitByBackKey() {

        AlertDialog alertbox = new AlertDialog.Builder(this)
        .setMessage("Do you want to exit application?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            // do something when the button is clicked
            public void onClick(DialogInterface arg0, int arg1) {

                finish();
                //close();


            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {

            // do something when the button is clicked
            public void onClick(DialogInterface arg0, int arg1) {
                           }
        })
          .show();

    }
0
Arjun Chauhan

時々活動は履歴を保持し、確認後に終了しません...私の場合は最善の解決策です。

private int k = 0;


@Override
public boolean onKeyDown(int keyCode, KeyEvent e) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {
        oNBackPressedExitApp();
        return true;
    }

    return super.onKeyDown(keyCode, e);
}

@Override
protected void onStart() {
    super.onStart();
    k = 0;
}


public void oNBackPressedExitApp(){
        k++;
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Exit Confirmation");
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.setMessage("Do you really want to exit the App?");

    if(k == 1) {
        alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                finish();
                Intent homeScreenIntent = new Intent(Intent.ACTION_MAIN);
                homeScreenIntent.addCategory(Intent.CATEGORY_HOME);
                homeScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(homeScreenIntent);
                return;
            }
        });
        alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                return;
            }
        });
        alertDialog.show();
    }
}
0
Firefog