【问题标题】:didn't work exit pop up backbutton code没有工作退出弹出后退按钮代码
【发布时间】:2014-04-30 14:41:24
【问题描述】:

我想在用户按下主屏幕上的返回按钮时显示弹出窗口。但它不起作用,并且没有错误。我使用此代码处理滑块菜单

  public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    AlertDialog.Builder build = new AlertDialog.Builder(this);
    build.setTitle("Confirmation");
    Log.d("aaaaa", "msg");
    build.setMessage("Are you sure, you want to exit ?");
    final AlertDialog alertDialog = build.create();
    alertDialog.show();
    build.setPositiveButton("Ha (Yes)", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            alertDialog.dismiss();
            finish();
        }
    });
    build.setNegativeButton("Na (No)", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            alertDialog.dismiss();
        }
    });

【问题讨论】:

    标签: android popup back-button exit-code


    【解决方案1】:

    在设置是/否按钮之前显示对话框。你需要把 alertDialog.show();在末尾。还要摆脱 super.backPressed()。你不需要它,因为你覆盖了这个原始功能。

    【讨论】:

      【解决方案2】:

      调用super.onBackPressed() 表示“正常处理后退按钮”(即完成活动)。

      如果你想拦截它并显示一个对话框(就像这种情况一样)不调用它就足够了。

      【讨论】:

      • 是的,它可以工作,但没有显示是/否选项,它只显示 MSg。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-25
      • 2014-10-07
      • 1970-01-01
      • 2011-01-22
      相关资源
      最近更新 更多