构造一个自定义的dialog,其中包括一个编辑栏(EditText)和两个按钮(确定和取消)
如果想要当EditText为空的时候让确定按钮为不可点击状态
private void createCustomDialog(Context dialogContext, final int position, String defaultNumber) {
final EditText editText = new EditText(dialogContext);
final AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);
builder.setView(editText);
builder.setTitle(R.string.fastdialer_add_number_title);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//任何你想做的事情
});
builder.setNegativeButton(android.R.string.cancel, null);
Dialog dialog = builder.create();
dialog.show();//!!!!!!!!!!!!!!看这里,先把dialog show出来。
final Button positiveButton = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE);
//现在这个dialog的button不会再是空的了!!!!!!!!!!!
if(editText!=null && editText.getText().toString().equals(""))
positiveButton.setEnabled(false);
customType.addTextChangedListener(
new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
if(arg0.toString().equals("")){
positiveButton.setEnabled(false);
} else {
positiveButton.setEnabled(true);
}
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
}
);
}