Android - 对话框 - 如果用户输入错误密码,则保持对话框打开

时间:2014-02-27 15:46:21

标签: android input dialog

我有一个对话框供用户输入解锁码。如果他们输入错误的代码,我想保持打开框,这样他们就有机会修复它。

AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Unlock Code");
    alert.setMessage("Please enter the unlock code.");


    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();
     if(codeUnlock.checkCode(value)){ // checks the code that was put in
         Toast.makeText(MainActivity.this, "Thank you for purchasing.", Toast.LENGTH_LONG).show();
         yearPurchased = currentYear;
         checkForUpdate(false);

     }else{
         Toast.makeText(MainActivity.this, "Incorrect code.", Toast.LENGTH_LONG).show();


     }


      }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {

      }
    });

    alert.show();

基本上,如果checkCode为false,我想jsut显示吐司但不关闭窗口。

有什么建议吗?

谢谢。

4 个答案:

答案 0 :(得分:1)

也许这有帮助。只需添加一个onShowListener就可以了。我自己没有测试过,但从这里得到它:https://stackoverflow.com/a/7636468。但它只适用于API 8 +。

alert.setPositiveButton(android.R.string.ok, null);
alert.setOnShowListener(new DialogInterface.OnShowListener() {

@Override
public void onShow(DialogInterface dialog) {

    Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // Do something

            // Dismiss once everything is OK.
            d.dismiss();
        }
    });
}

答案 1 :(得分:1)

你可以试试这个:

builder.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
    if(!errorFlag) {
             // you need this flag in order to close the dialog 
             // when there is no issue
             dialog.dismiss();
     }
}
});

设置肯定按钮:

builder.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
   Button b = builder.getButton(AlertDialog.BUTTON_POSITIVE);
       b.setOnClickListener(new View.OnClickListener() {

    @Override
public void onClick(View view) {
        // perform operations here
        // set the flag, that is isError = true or false
        // if false, call builder.dismiss();
    }
  }
});

设置否定按钮:

Button n = builder.getButton(AlertDialog.BUTTON_NEGATIVE);
n.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
        // perform operations here too if needed.
     }
    }
   });
  }
});

创建如下对话框:

final AlertDialog builder = new AlertDialog.Builder(YourActivity.this)
    .setNegativeButton("cancel", null)
    .setPositiveButton("ok", null)
    .create();

使用以下方式显示对话框:

builder.show();

答案 2 :(得分:1)

昨天我遇到了这个问题,我通过覆盖正面按钮的点击监听器解决了这个问题:

AlertDialog.Builder builder = new AlertDialog.Builder(
            context);
    builder.setTitle(R.string.my_title);
    builder.setView(getLayoutInflater().inflate(
            R.layout.my_dialog, null));
    builder.setPositiveButton(android.R.string.ok, null);
    builder.setNegativeButton(android.R.string.cancel, null);
    final AlertDialog dialog = builder.create();
    dialog.show();
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText editTextUserId = (EditText) dialog
                            .findViewById(R.id.editTextUserId);
                    EditText editTextPassword = (EditText) dialog
                            .findViewById(R.id.editTextPassword);

                    if (editTextUserId.length() == 0
                            || editTextPassword.length() == 0) {
                        Toast.makeText(
                                context,
                                R.string.you_must_enter_username_and_password,
                                Toast.LENGTH_LONG).show();
                        return;
                    }

                    dialog.dismiss();
                }
            });

答案 3 :(得分:1)

我知道我在派对上已经很晚了,但希望这可以帮助人们浏览Google。我的解决方案要求如果用户输入错误的内容,则抛出IOException。

流程图:

Flow Chart

我就这样做了:

用户在对话框中输入输入,然后按下正按钮

builder.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        try {
            //do stuff if input was correct
            //if not, then throw an IOException
            dialog.dismiss();
            //dismiss, dialog goes away
        } catch (IOException e) {
            //User entered bad input, change the form to show feedback
            AlertDialog thisDialog = (AlertDialog) dialog;
            thisDialog.setTitle("Incorrect Format");
            thisDialog.setIcon(android.R.drawable.ic_dialog_alert);
            thisDialog.cancel();
            //cancel the dialog -> fire the OnCancelListener
        }
    }
});

假设它不正确,OnCancelListener被解雇

builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        ((AlertDialog) dialog).show();
    }
});

这让我们回到正面按钮。

万一我们希望他们实际取消对话而不进入无限循环,当他们按下否定按钮时,我们会在那里解除对话。

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
});

真的希望这有帮助!