我怎么能把"你确定要退出吗?"当我按下按钮android

时间:2015-03-31 08:45:21

标签: android back

我想发一条消息"你确定要退出吗?"和"是" - "不"从我的应用程序中按下" BACK"按钮......有可能吗?我怎么办呢?感谢

3 个答案:

答案 0 :(得分:11)

是的,通过反击您的活动,可以通过alertdialog

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
           .setMessage("Are you sure you want to exit?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    ExampleActivity.this.finish();
               }
           })
           .setNegativeButton("No", null)
           .show();
}

答案 1 :(得分:1)

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
        .setTitle("Really Exit?")
        .setMessage("Are you sure you want to exit?")
        .setNegativeButton(android.R.string.no, null)
        .setPositiveButton(android.R.string.yes, new OnClickListener() {

            public void onClick(DialogInterface arg0, int arg1) {
                WelcomeActivity.super.onBackPressed();
            }
        }).create().show();
}

取自this回答。

答案 2 :(得分:0)

中添加用于提醒对话的代码段
@Override
    public void onBackPressed() {
    final AlertDialog alert = new AlertDialog.Builder(new ContextThemeWrapper(contextForAlert,R.style.CustomAlertDialogue)).create();
    alert.setTitle(title);
    alert.setMessage(message);
    alert.setIcon(R.drawable.warning_icon);
    alert.setCancelable(false);
    alert.setCanceledOnTouchOutside(false);
    alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {



    }
    });


    alert.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            alert.dismiss();    

         }
        });



    alert.setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {


            }
        });

    alert.show();
    }