您好我有以下两个函数,并想知道是否可以阻止下面的“正面按钮”取决于布尔值是true还是false(如果用户在EditText中输入了文本)?
private void add() {
final View addView = getLayoutInflater().inflate(R.layout.add, null);
new AlertDialog.Builder(this).setTitle("Add a Book").setView(addView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
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)
}
}
}).setNegativeButton("Cancel", null).show();
}
private boolean addWord(EditText title){
String mDisplaySting = title.getText().toString();
if(mDisplaySting.matches("")){
Log.i(TAG,"null");
return false;
}
return true;
}
答案 0 :(得分:0)
您可以按如下方式执行禁用:
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);
}
}
您也可以尝试使用其他方式阻止按钮,现在您可以访问它。
答案 1 :(得分:0)
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();
像这样的东西。没有测试它。