只需使用神经按钮
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(super.mContext);
dialogBuilder.setTitle(
super.mContext.getResources().getString(R.string.FAVORITE_RENAME_DIALOG_TITLE));
dialogBuilder.setMessage(
super.mContext.getResources().getString(R.string.FAVORITE_RENAME_DIALOG_MESSAGE));
final EditText input = new EditText(super.mContext);
input.setInputType(
InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_VARIATION_LONG_MESSAGE);
input.setText(name);
input.setSelectAllOnFocus(true);
input.setHint(super.mContext.getResources().getString(R.string.FAVORITE_DIALOG_HINT));
input.setFilters(
new InputFilter[]{new DialogInputFilter(), new InputFilter.LengthFilter(20)});
dialogBuilder.setView(input);
dialogBuilder.setNeutralButton(
super.mContext.getResources().getString(R.string.FAVORITE_RENAME_DIALOG_POSITIVE),
new UpdateFavoriteListNameOnClickListener(name, favListId, input,
(Activity) super.mContext, this)
);
dialogBuilder.setNegativeButton(
super.mContext.getResources().getString(R.string.FAVORITE_DIALOG_NEGATIVE),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
}
);
首先在这里 Create() 然后获取 alertDialog 并获取 Button 并使用它做你想要的......我将它用于 MinLength 验证
dialogBuilder.create();
final AlertDialog alertDialog = dialogBuilder.show();
Button button = ((AlertDialog)alertDialog).getButton(AlertDialog.BUTTON_NEUTRAL);
input.addTextChangedListener(new TextMinLengthValidator(input, button) {
@Override
public void validate(EditText input, Button button) {
if (input.getText().length() < ConfigurationProvider.getConfig().getMinFavoriteNameLength() && button != null) {
button.setEnabled(false);
input.setError(mContext.getResources().getString(R.string.FAVORITE_DIALOG_LENGTH_NEGATIVE));
} else if(button != null) {
button.setEnabled(true);
input.setError(null);
}
}
});