我正在尝试禁用警报对话框,如果TextView为空,我浏览了几个帖子,但不是它们是自定义警报对话框。 怎么解决这个问题。提前致谢 以下是我的代码示例自定义提醒对话框代码
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(Nav_Settings.this);
View promptView = layoutInflater.inflate(R.layout.nav_settings_change_password, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Nav_Settings.this);
alertDialogBuilder.setView(promptView);
alertDialogBuilder.setIcon(R.drawable.icon_secret);
alertDialogBuilder.setTitle("CHANGE PASSWORD");
final EditText currentPassword = (EditText) promptView.findViewById(R.id.etCurrentPassword);
final EditText newPassword = (EditText) promptView.findViewById(R.id.etNewPassword);
final EditText confirmNewPassword = (EditText) promptView.findViewById(R.id.etConfirmNewPassword);
// setup a dialog window
alertDialogBuilder.setPositiveButton("Update Password", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (TextUtils.isEmpty(currentPassword.getText().toString().trim())) {
currentPassword.setError("should not be empty");
} else if (TextUtils.isEmpty(newPassword.getText().toString().trim())) {
newPassword.setError("should not be empty");
} else if (TextUtils.isEmpty(confirmNewPassword.getText().toString().trim())) {
confirmNewPassword.setError("should not be empty");
} else if (newPassword.getText().toString().trim() != confirmNewPassword.getText().toString().trim()) {
confirmNewPassword.setError("new password not matching");
} else if (currentPassword == newPassword) {
newPassword.setError("new password should not be current password");
} else {
Toast.makeText(getApplicationContext(), currentPassword.getText().toString() + "\n" + newPassword.getText().toString() + "\n" + confirmNewPassword.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
}).setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
alert.getButton(alert.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
alert.getButton(alert.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/RobotoCondensed-Bold.ttf");
alert.getButton(alert.BUTTON_POSITIVE).setTypeface(face);
alert.getButton(alert.BUTTON_NEGATIVE).setTypeface(face);
}
答案 0 :(得分:0)
最初将其设为禁用:
Button button = alert.getButton(alert.BUTTON_NEGATIVE);
button.setEnabled(false);
alert.setCancelable(false);
在密码EditText上添加TextWhatcher:
currentPassword.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) {
if(TextUtils.isEmpty(currentPassword.getText().toString().trim())){
button.setEnabled(false);
alert.setCancelable(false);
} else{
button.setEnabled(true);
alert.setCancelable(true);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
基本上,每次用户更改EditText中的文本时,您都可以检查它是否为空&相应地启用/禁用“取消”按钮。
答案 1 :(得分:0)
您可以在方法顶部放置一个布尔值,只有在清除所有验证后才能使其成为现实
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(Nav_Settings.this);
View promptView = layoutInflater.inflate(R.layout.nav_settings_change_password, null);
final boolean isCloseValid = false; // THIS BOOLEAN
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Nav_Settings.this);
alertDialogBuilder.setView(promptView);
alertDialogBuilder.setIcon(R.drawable.icon_secret);
alertDialogBuilder.setTitle("CHANGE PASSWORD");
final EditText currentPassword = (EditText) promptView.findViewById(R.id.etCurrentPassword);
final EditText newPassword = (EditText) promptView.findViewById(R.id.etNewPassword);
final EditText confirmNewPassword = (EditText) promptView.findViewById(R.id.etConfirmNewPassword);
// setup a dialog window
alertDialogBuilder.setPositiveButton("Update Password", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (TextUtils.isEmpty(currentPassword.getText().toString().trim())) {
currentPassword.setError("should not be empty");
isValidClose = false // VALIDATION FALSE
} else if (TextUtils.isEmpty(newPassword.getText().toString().trim())) {
newPassword.setError("should not be empty");
isValidClose = false // VALIDATION FALSE
} else if (TextUtils.isEmpty(confirmNewPassword.getText().toString().trim())) {
confirmNewPassword.setError("should not be empty");
isValidClose = false // VALIDATION FALSE
} else if (newPassword.getText().toString().trim() != confirmNewPassword.getText().toString().trim()) {
confirmNewPassword.setError("new password not matching");
isValidClose = false // VALIDATION FALSE
} else if (currentPassword == newPassword) {
newPassword.setError("new password should not be current password");
isValidClose = false // VALIDATION FALSE
} else {
Toast.makeText(getApplicationContext(), currentPassword.getText().toString() + "\n" + newPassword.getText().toString() + "\n" + confirmNewPassword.getText().toString(), Toast.LENGTH_SHORT).show();
isValidClose = true; // MAKE TRUE HERE
}
}
}).setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(isValidClose){ // if validations is cleared
dialog.cancel();
}
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
alert.getButton(alert.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
alert.getButton(alert.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/RobotoCondensed-Bold.ttf");
alert.getButton(alert.BUTTON_POSITIVE).setTypeface(face);
alert.getButton(alert.BUTTON_NEGATIVE).setTypeface(face);
}