Android 禁用AlertDialog按钮?
您好,我有以下两个函数,我想知道是否有可能根据布尔值阻止下面的“正按钮”为真或假(如果用户在EditText中输入了文本或未输入文本)Android 禁用AlertDialog按钮?,android,android-layout,Android,Android Layout,您好,我有以下两个函数,我想知道是否有可能根据布尔值阻止下面的“正按钮”为真或假(如果用户在EditText中输入了文本或未输入文本) 您可以按如下方式执行禁用操作: public void onClick(DialogInterface dialog, int whichButton) { if(addWord((EditText) addView.findViewById(R.id.titleEdit))){ // Do something, Enable the OK (
您可以按如下方式执行禁用操作:
public void onClick(DialogInterface dialog, int whichButton) {
if(addWord((EditText) addView.findViewById(R.id.titleEdit))){
// Do something, Enable the OK (Positive) button
} else {
Toast.makeText(ActionBarMain.this, "Nothing entered",
Toast.LENGTH_LONG).show();
//Prevent the user to be able to push the "PositiveButton" (Block it)
AlertDialog myDialog = (AlertDialog)dialog;
Button button = myDialog.getButton(whichButton);
button.setOnClickListener(null);
}
}
您还可以尝试其他方法来阻止按钮,现在您可以访问它了
AlertDialog mAlertDialog = new AlertDialog.Builder(this)
.setTitle("Add a Book").setView(addView)
.setNegativeButton("Cancel", null);
if(!edittext.getText().toString().equals("")){
mAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
[...]
}
});
}
mAlertDialog.show();
像这样的。没有测试它。如何创建DialogBuilder对象(“AlertDialog.Builder b=new…”而不是“new AlertDialog.Builder…”),然后在if条件下添加肯定按钮?
AlertDialog mAlertDialog = new AlertDialog.Builder(this)
.setTitle("Add a Book").setView(addView)
.setNegativeButton("Cancel", null);
if(!edittext.getText().toString().equals("")){
mAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
[...]
}
});
}
mAlertDialog.show();