【问题标题】:Issues in onBackpressed()onBackpressed() 中的问题
【发布时间】:2016-07-11 11:07:32
【问题描述】:

实际上我的问题是当我按下后退按钮时它显示dialog 并且同时应用程序在没有任何键操作的情况下完成?谁能解决这个问题?

@Override
public void onBackPressed() {
    super.onBackPressed();
    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setMessage("Really want to exit");
    ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Main.super.onBackPressed();
        }
    });
    ab.create();
    ab.show();
}

【问题讨论】:

    标签: java android onbackpressed


    【解决方案1】:

    从方法中删除super.onBackPressed();,只保留在onClick()中

    @Override
    public void onBackPressed() {
        super.onBackPressed(); //remove it
    
    }
    

    您似乎在两个按钮上执行相同的操作,您应该从 negativeButton 中删除 Main.super.onBackPressed()

    ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            super.onBackPressed();
        }
    });
    ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ab.dismiss();
        }
    });
    

    【讨论】:

      【解决方案2】:

      不要调用onBackPressed的超级方法。

      @Override
      public void onBackPressed() {
      
          AlertDialog.Builder ab = new AlertDialog.Builder(this);
          ab.setMessage("Really want to exit");
          ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  finish();
              }
          });
          ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
      
              }
          });
          ab.create();
          ab.show();
      }
      

      【讨论】:

      • 如果我按下否,应用程序也关闭了,如果我按下否,如何限制该活动
      • @A.asha 检查我更新的答案。删除这个 Main.super.onBackPressed();
      【解决方案3】:
      try this
        @Override
          public void onBackPressed() {
      
              AlertDialog.Builder ab = new AlertDialog.Builder(this);
              ab.setMessage("Really want to exit");
              ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                     finish();
                  }
              });
              ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      Main.super.onBackPressed();
                  }
              });
              ab.create();
              ab.show();
          }
      

      【讨论】:

        【解决方案4】:

        您需要从onBackPressed() 中删除super.onBackPressed();。那么你的功能应该是,

        @Override
        public void onBackPressed() {
        
        AlertDialog.Builder ab = new AlertDialog.Builder(this);
        ab.setMessage("Really want to exit");
        ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Main.super.onBackPressed();
            }
        });
        ab.create();
        ab.show();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多