本文介绍了AlertDialog - 我怎么能运行检查时,用户点击“确定”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关自定义AlertDialog,可我覆盖了积极的按钮不关闭对话框?相反,我要运行一些编辑检查并保持对话,如果我检查失败开放。

For a custom AlertDialog, can I override the positive button to NOT close the dialog? instead I want to run some edit checks and keep the dialog open if my checks fail.

protected Dialog onCreateDialog(int id) {
  Dialog alertDialog = null;
  builder = new AlertDialog.Builder(this);
  switch(id) {
    case LOGIN_USERID_BLANK:
      builder.setMessage((String)getString(R.string.username_not_blank));
      builder.setPositiveButton((String)getString(R.string.ok), new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
      // Can I do something here so that the dialog does not close?
}
});

突破;

推荐答案

下面是一个解决办法,直到谷歌改变了对话框API:

Here's a workaround until Google changes the Dialog API:

LayoutInflater factory = LayoutInflater.from(this);
final View view = factory.inflate(R.layout.yoMamma, null);
final Dialog dlg = new AlertDialog.Builder(this)
    .setTitle(R.string.yoTitle)
    .setView(view)
    .setPositiveButton(R.string.dlgOK, new DialogInterface.OnClickListener() {
      @Override public void onClick(DialogInterface dialog, int which) {
        // This won't be called.
      }})
    .create();
dlg.show();
View button = ((AlertDialog)dlg).getButton(DialogInterface.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
  @Override public void onClick(View v) {
    // This WILL be called.
    // Remove the following if you don't want the dialog to dismiss
    dlg.dismiss();
  }});

这篇关于AlertDialog - 我怎么能运行检查时,用户点击“确定”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:55