我有一个多选对话框,但我有一个小问题。这是代码:
int in;
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]);
in = 0;
DialogInterface.OnMultiChoiceClickListener measurementsDialogListener = new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecselectedMeasurementsChecked){
if(isChecselectedMeasurementsChecked)
if(in<3){
selectedMeasurements.add(measurements[which]);
in++;
}
else{
selectedMeasurements.remove(measurements[which]);
in--;
}
onChangeSelectedMeasurements();
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Measurements");
builder.setMultiChoiceItems(measurements, checkedMeasurements, measurementsDialogListener);
AlertDialog dialog = builder.create();
dialog.show();
}
protected void onChangeSelectedMeasurements() {
StringBuilder stringBuilder = new StringBuilder();
for(CharSequence colour : selectedMeasurements)
stringBuilder.append(colour + ",");
}
一切都很好,我有一个包含一些元素的列表。但我想添加不能选择超过 3 个元素的功能。现在我可以从列表中选择所有元素。我想实现我只能选择 3 个元素。我怎样才能做到这一点?