请看下面的自定义对话框。我在对话框中有一个编辑文本字段,如果文本字段为空,我想禁用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();
}
发布于 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);
发布于 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);
}
}
});
发布于 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;
}
};
https://stackoverflow.com/questions/8238952
复制相似问题