【问题标题】:set password on back pressed后按设置密码
【发布时间】:2018-03-10 04:54:38
【问题描述】:

我关注this提示密码提示

所以我已经给了这个,但它不起作用

 @Override
public void onBackPressed() {
 showDialog();
}

但它不工作onBackPressed 它返回或退出应用程序...

它如何与onBackPressed alert 一起正常工作

@Override
public void onBackPressed() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("Do You Want to Exit ?");
    alertDialogBuilder
            .setMessage("Click Yes to Exit!")
            .setCancelable(false)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            moveTaskToBack(true);
                            android.os.Process.killProcess(android.os.Process.myPid());
                            System.exit(0);
                        }
                    })

            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

但我想要提示密码onBackPressed

谁能帮我onBackPressed的密码有什么问题?

【问题讨论】:

    标签: android passwords alert


    【解决方案1】:

    你好,一旦尝试这个对我来说效果很好......

    提供直接警报而不是布局

    @Override
    public void onBackPressed() {
    
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Title");
        alert.setMessage("Message");
    
        final EditText input = new EditText(this);
        alert.setView(input);
    
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String value = input.getText().toString();
                // Do something with value!
    
    
                /** CHECK FOR USER'S INPUT **/
                if (value.equals("oeg"))
                {
                                    /*Log.d(input, "HELLO THIS IS THE MESSAGE CAUGHT :)");
                                    Search_Tips(input);*/
                    finish();
    
                }
                else{
                    //Log.d(input,"string is empty");
                    String message = "The password you have entered is incorrect." + " \n \n" + "Please try again!";
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Error");
                    builder.setMessage(message);
                    builder.setPositiveButton("Cancel", null);
                    /*builder.setNegativeButton("Retry", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            //showDialog();
                        }
                    });*/
                    builder.create().show();
    
                }
            }
        });
    
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // Canceled.
            }
        });
    
        alert.show();
    
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

           @Override
          public void onBackPressed() {
              if (isFirsttym) {
                  super.onBackPressed();
                  return;
              }
      
      //        this.isFirsttym = true;
              showDialog();
      
      //        new Handler().postDelayed(new Runnable() {
      //
      //            @Override
      //            public void run() {
      //                isFirsttym=false;
      //            }
      //        }, 5000);
          }
      
      
          public void showDialog()
          {
      
              LayoutInflater li = LayoutInflater.from(this);
              View promptsView = li.inflate(R.layout.cust_dialog, null);
              final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
              alertDialogBuilder.setView(promptsView);
      
              final EditText userInput = (EditText) promptsView
                      .findViewById(R.id.user_input);
      
      
              // set dialog message
              alertDialogBuilder
                      .setCancelable(false)
                      .setNegativeButton("Go",
                              new DialogInterface.OnClickListener() {
                                  public void onClick(DialogInterface dialog,int id) {
                                      /** DO THE METHOD HERE WHEN PROCEED IS CLICKED*/
                                      String user_text = (userInput.getText()).toString();
      
                                      /** CHECK FOR USER'S INPUT **/
                                      if (user_text.equals("oeg"))
                                      {
                                          Log.d(user_text, "HELLO THIS IS THE MESSAGE CAUGHT :)");
      //                                    Search_Tips(user_text);
                                          isFirsttym=true;
      
                                      }
                                      else{
                                          Log.d(user_text,"string is empty");
                                          String message = "The password you have entered is incorrect." + " \n \n" + "Please try again!";
                                          AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
                                          builder.setTitle("Error");
                                          builder.setMessage(message);
                                          builder.setPositiveButton("Cancel", null);
                                          builder.setNegativeButton("Retry", new DialogInterface.OnClickListener() {
                                              @Override
                                              public void onClick(DialogInterface dialog, int id) {
                                                  showDialog();
                                                  isFirsttym=false;
      
                                              }
                                          });
                                          builder.create().show();
      
                                      }
                                  }
                              })
                      .setPositiveButton("Cancel",
                              new DialogInterface.OnClickListener() {
                                  public void onClick(DialogInterface dialog,int id) {
                                      dialog.dismiss();
                                      isFirsttym=false;
                                  }
      
                              }
      
                      );
      
              // create alert dialog
              AlertDialog alertDialog = alertDialogBuilder.create();
      
              // show it
              alertDialog.show();
      
          }
      

      【讨论】:

      • 感谢用户@Vishva Dava 的回复......但在后面按下它也不起作用。
      • 我不知道为什么如果我复制粘贴你的代码它显示警报......但它不能为错误的密码工作它退出......如果我在 showDialog() 提供我的代码它甚至不工作。 ..但是您的代码显示警报,但在输入错误后它也退出了...(我已删除代码中的注释行)
      • 相同但我发现问题出在哪里..如果我按下它显示的警报..好的…如果我点击取消…然后按下它的退出..它的工作方式与错误密码相同还有……
      • No 与 MianActivity.this、getApplicationContext()、this.. 仍然相同
      • user@Vishva Dave 你的回答也有效.. 但我需要在 if 条件下添加fishis();.. 我会支持你的其他答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2020-02-01
      • 2012-06-04
      • 2011-06-30
      相关资源
      最近更新 更多