我使用 AlertDialog.Builder 创建多选对话框
mDialog = new AlertDialog.Builder(getActivity()).setIconAttribute(mIcon).setTitle(mTitle)
.setPositiveButton(mPositiveButton, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mCallbacks.onPositiveClick(((AlertDialog) getDialog()).getListView().getCheckedItemPositions(), mChoices);
}
}).setNegativeButton(mNegativeButton, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mCallbacks.onNegativeClick(((AlertDialog) getDialog()).getListView().getCheckedItemPositions());
}
}).setMultiChoiceItems(mChoices, mCheckedItems, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (mIsCheckedRequired) {
AlertDialog alertDialog = ((AlertDialog) dialog);
if (alertDialog.getListView().getCheckedItemCount() == 0)
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
else
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
}
}).create();
我还想在没有检查项目时禁用积极按钮:
@Override
public void onStart() {
super.onStart();
if (mIsCheckedRequired && mDialog.getListView().getCheckedItemCount() == 0) {
mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
}
mDialog.getListView().getCheckedItemCount() 在点击之前总是返回 0,有没有办法在点击之前知道检查项目的数量,除了字段 mCheckedItems 数组并运行它?