【发布时间】:2022-01-11 01:48:38
【问题描述】:
当输入字段之一为空时,我想禁用肯定按钮。
我尝试使用((AlertDialog)dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
但它不起作用。
new AlertDialog.Builder(MainActivity.this)
.setView(viewInput)
.setTitle("Add task")
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String title = edtTitle.getText().toString();
String des = edtDes.getText().toString();
String date = edtDate.getText().toString();
String time = edtTime.getText().toString();
if(isEmpty(title) || isEmpty(des) || isEmpty(date) || isEmpty(time)){
((AlertDialog)dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
Toast.makeText(MainActivity.this,"Please, enter all fields.",Toast.LENGTH_SHORT).show();
}else{
Task task = new Task(title, des, date, time);
boolean isInserted = new TaskHandler(MainActivity.this).create(task);
if(isInserted){
Toast.makeText(MainActivity.this,"Task Saved",Toast.LENGTH_SHORT).show();
loadTasks();
}else{
Toast.makeText(MainActivity.this,"Unable to save",Toast.LENGTH_SHORT).show();
}
dialogInterface.cancel();
}
}
}).show();
【问题讨论】:
标签: java android-studio android-alertdialog