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

时间:2018-03-09 06:47:08

标签: android

如何在执行下一个任务之前在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 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

尝试使用以下代码:

参考代码:

 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
       }

 }

答案 2 :(得分:0)

我认为你的意思是说除非密码正确,否则不要关闭对话框。如果是这样,试试这段代码

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

答案 3 :(得分:0)

我认为问题是默认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 
        }
    });