【问题标题】:Android - Dialog Box - Leave dialog open if user inputs wrong passwordAndroid - 对话框 - 如果用户输入错误的密码,则保持对话框打开
【发布时间】:2014-02-27 15:46:20
【问题描述】:

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

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,我只想显示 toast 但不关闭窗口。

有什么建议吗?

谢谢。

【问题讨论】:

  • 你不能通过警告对话框来做到这一点。 AlertDialog 中的所有set*Button 都会关闭对话框。您需要使用带有您自己的布局和按钮的对话框。
  • 你试过setCancelable(false)吗?
  • 为什么不制作自定义对话框类,扩展对话框,只有在密码正确时才调用 dismis()?
  • 是的。所有好的建议。我希望有一个更简单的方法...

标签: android input dialog


【解决方案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】:

    也许这会有所帮助。只需添加一个 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();
            }
        });
    }
    

    【讨论】:

      【解决方案3】:

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

      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();
                      }
                  });
      

      【讨论】:

        【解决方案4】:

        我知道我迟到了,但希望这对浏览 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();
            }
        });
        

        真的希望这会有所帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-15
          • 1970-01-01
          • 1970-01-01
          • 2011-09-02
          • 1970-01-01
          • 2011-05-10
          相关资源
          最近更新 更多