代码之家  ›  专栏  ›  技术社区  ›  Alvin

在执行下一个任务之前,警告对话框密码验证

  •  1
  • Alvin  · 技术社区  · 8 年前

    在执行下一个任务之前,如何在alertdialog(警报对话框)中验证密码?

    @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.insert_btn:
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Looper.prepare();
                            makeHistory();
                            Looper.loop();
                        }
                    }).start();
                    break;
            }
        }
    
    private void makeHistory(){
    
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("PASSWORD");
            final EditText input = new EditText(this);
            input.setInputType(InputType.TYPE_CLASS_TEXT | 
        InputType.TYPE_TEXT_VARIATION_PASSWORD);
            builder.setView(input);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() 
            {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mPassword = input.getText().toString();
                    // ****** stop proceeding to next task if password is incorrect *******
                   if(mPassword == true) .....
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    return;
                }
            });
            builder.show();
            // ***** next task here ******
            Log.d("Stop", "I dont want this to be print if password is incorrect");
    }
    
    4 回复  |  直到 8 年前
        1
  •  1
  •   Navneet Krishna    8 年前

    根据api调用返回的响应,检查密码是否正确,然后使用 if else 检查以决定是否执行 下一步任务 。您可能将下一个任务的内容放错了在警报对话框之后。请参见以下示例

    if(mPassword.equals("password returned from server")){
     executeNextTasks();
    } else {
    // when wrong password execute something
    }
    

    在…内 executeNextTasks 添加密码正确时要执行的任何代码

        2
  •  0
  •   Abhishek kumar    8 年前

    尝试以下代码:

    此处的参考代码:

     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() 
            {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mPassword = input.getText().toString();
    
                    //API call from Here 
                    if (utils.haveNetworkConnection()) {
                        if (!input.getText().toString().isEmpty()) {
                            jsonRequestLogin(mPassword);
    
                        }else{
                            input.setError("Enter Password");     
                      }
                    } else {
                        utils.showtoast("Internet Connection Not Available");
                    }
    
                }
            });
    
    
    
     //API call here
     private void jsonRequestLogin(String mPassword) {
    
           if (code.equals("1")) {  //Correct Password
               //Go to next Activity
           }else{
              //In correct Password
           }
    
     }
    
        3
  •  0
  •   Android_K.Doe    8 年前

    我想你的意思是说除非密码正确,否则不要关闭对话框。如果是,请尝试此代码

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("PASSWORD");
    builder.setView(input);
    builder.setPositiveButton("OK", null);
    builder.setNegativeButton("Cancel", null);
    
    AlertDialog dialog = builder.create();
    dialog.show();
    
    dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(!input.equals(password)){
               input.requestFocus();
               input.setError("Incorrect Password");
            }else{
              dialog.dismiss();   
              doNext();
            }
        }
    });
    

    然后按“取消”按钮,只需呼叫

    dialog.dismiss
    
        4
  •  0
  •   ADM    8 年前

    我认为问题在于违约 AlertDialog 行为 DialogInterface.OnClickListener 无论您的代码是什么,都将关闭对话框。所以你需要预防它。下面是一个示例。

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Some random message");
        builder.setPositiveButton("Check",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Useless
                    }
                });
        final AlertDialog dialog = builder.create();
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Do your stuff here 
            }
        });