我想知道如何使用只能选择 4 个复选框的元素列表来实现对话框。我怎样才能做到这一点?有教程吗?我有类似的东西:
protected void showSelectMeasurementsDialog() {
boolean[] checkedMeasurements = new boolean[measurements.length];
int count = measurements.length;
for(int i = 0; i < count; i++)
checkedMeasurements[i] = selectedMeasurements.contains(measurements[i]);
DialogInterface.OnMultiChoiceClickListener measurementsDialogListener = new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecselectedMeasurementsChecked){
if(isChecselectedMeasurementsChecked)
selectedMeasurements.add(measurements[which]);
else{
selectedMeasurements.remove(measurements[which]);
}
onChangeSelectedMeasurements();
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Measurements");
builder.setMultiChoiceItems(measurements, checkedMeasurements, measurementsDialogListener);
AlertDialog dialog = builder.create();
dialog.show();
}
但我不知道当我选择了 4 个项目时如何阻止选择项目?