首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何禁用/启用对话框负向按钮?

如何禁用/启用对话框负向按钮?
EN

Stack Overflow用户
提问于 2011-11-23 16:12:45
回答 5查看 92.1K关注 0票数 119

请看下面的自定义对话框。我在对话框中有一个编辑文本字段,如果文本字段为空,我想禁用positiveButton。我可以获得文本字段的charListener,但我不确定如何从该侦听器中将positivebutton设置为禁用或启用?正负按钮的参照是什么?

 case DIALOG_TEXT_ENTRY:
    // This example shows how to add a custom layout to an AlertDialog
    LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
    return new AlertDialog.Builder(AlertDialogSamples.this)
        .setIconAttribute(android.R.attr.alertDialogIcon)
        .setTitle(R.string.alert_dialog_text_entry)
        .setView(textEntryView)
        .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked OK so do some stuff */
            }
        })
        .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked cancel so do some stuff */
            }
        })
        .create();
}
EN

回答 5

Stack Overflow用户

发布于 2013-05-07 14:33:38

这些答案都不能真正解决问题。

我使用一个自定义布局来实现这一点,其中包含一个EditText,并且在该视图上有一个TextWatcher。

final LinearLayout layout = (LinearLayout) inflator.inflate(R.layout.text_dialog, null);
final EditText text = (EditText) layout.findViewById(R.id.text_edit);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
// Now add the buttons...
builder.setPositiveButton(R.string.ok, new AlertDialog.OnClickListener() {
    // Left out for brevity...
}
builder.setNegativeButton(R.string.cancel, new AlertDialog.OnClickListener() {
    // Left out for brevity...
}

// Create the dialog
final AlertDialog d = builder.create();

// Now add a TextWatcher that will handle enable/disable of save button
text.addTextChangedListener(new TextWatcher() {
    private void handleText() {
        // Grab the button
        final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE);
        if(text.getText().length() == 0) {
            okButton.setEnabled(false);
        } else {
            okButton.setEnabled(true);
        }
    }
    @Override
    public void afterTextChanged(Editable arg0) {
        handleText();
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // Nothing to do
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
       // Nothing to do
    }
});

// show the dialog
d.show();
// and disable the button to start with
d.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
票数 27
EN

Stack Overflow用户

发布于 2016-03-22 18:21:05

下面是启用和禁用对话框正向按钮的完整代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater layoutInflater = MainActivity.this.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.dialog,null);

builder.setView(view);
builder.setTitle("Test");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this, "Ok clicked", Toast.LENGTH_SHORT).show();
        dialog.dismiss();
    }
});
builder.setNegativeButton("cancel", null);

final AlertDialog alertDialog = builder.create();

alertDialog.show();

EditText editText = (EditText)view.findViewById(R.id.mobile_number);
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    @Override
    public void afterTextChanged(Editable s) {
        if (s.length() >= 1) {
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
        } else {
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

        }
    }
});
票数 4
EN

Stack Overflow用户

发布于 2020-01-09 15:41:59

您可以在编辑文本框中编写侦听器,并尝试启用或禁用按钮。这是xamarin的示例代码。

var dialog = builder.Create();

dialog.Show();

var btnOk = dialog.GetButton((int)DialogButtonType.Positive).Enabled = false;

_enterTextDialogEditText.AfterTextChanged += (sender, e) => {
  if (!string.IsNullOrEmpty(_enterTextDialogEditText.Text)) {
    dialog.GetButton((int)DialogButtonType.Positive).Enabled = true;
  } else {
    dialog.GetButton((int)DialogButtonType.Positive).Enabled = false;
  }
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8238952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档