0
我有一個multichoice對話框,但是我有一個小問題。這是代碼:Android和多選對話框
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個元素。我怎樣才能做到這一點?
我知道應該如何實現這一目標,但是我需要解決方案我該如何在代碼中實現這一點。 – user1302569 2013-03-22 18:54:18