我有12个复选框,我需要确保用户只选择其中的5个,然后让5个选项标签输入5个不同的字符串。 我怎么能这样做?
这是我的XML文件:
<CheckBox
android:id="@+id/chkInterestsMovies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="223dp"
android:layout_y="257dp"
android:text="סרטים" />
<CheckBox
android:id="@+id/chkInterestsAnimals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="222dp"
android:layout_y="306dp"
android:text="חיות" />
<CheckBox
android:id="@+id/chkInterestsShopping"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="222dp"
android:layout_y="356dp"
android:text="קניות" />
<CheckBox
android:id="@+id/chkInterestsBooks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="149dp"
android:layout_y="257dp"
android:text="ספרים" />
<CheckBox
android:id="@+id/chkInterestsRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="147dp"
android:layout_y="305dp"
android:text="מסעדות" />
<CheckBox
android:id="@+id/chkInterestsComputers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="153dp"
android:layout_y="356dp"
android:text="מחשבים" />
<CheckBox
android:id="@+id/chkInterestsTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="72dp"
android:layout_y="255dp"
android:text="טלויזיה" />
<CheckBox
android:id="@+id/chkInterestsPubs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="76dp"
android:layout_y="306dp"
android:text="פאבים" />
<CheckBox
android:id="@+id/chkInterestsDancing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="76dp"
android:layout_y="355dp"
android:text="ריקודים" />
<CheckBox
android:id="@+id/chkInterestsMusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="7dp"
android:layout_y="257dp"
android:text="מוזיקה" />
<CheckBox
android:id="@+id/chkInterestsCoffe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="8dp"
android:layout_y="304dp"
android:text="בתי קפה" />
<CheckBox
android:id="@+id/chkInterestsOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="7dp"
android:layout_y="350dp"
android:text="אחר" />
答案 0 :(得分:0)
试试这个可能会对你有帮助。
int checkBoxCounter = 0;
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked && checkBoxCounter<6){
// apply check and increment check counter
checkBoxCounter++;
}else{
//todo nothing
}
}
答案 1 :(得分:0)
您还需要考虑选中和取消选中复选框。在任何给定的点,如果用户选择了5个,你可以继续。
粗略地说,在你的onCreate
中
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_LAYOUT);
//...
CheckBox checkboxMovies = (CheckBox)findViewById(R.id.chkInterestsMovies);
checkboxMovies.setOnCheckedChangeListener(this);
CheckBox checkboxAnimals = (CheckBox)findViewById(R.id.chkInterestsAnimals);
checkboxAnimals.setOnCheckedChangeListener(this);
CheckBox checkboxShopping = (CheckBox)findViewById(R.id.chkInterestsShopping);
checkboxShopping.setOnCheckedChangeListener(this);
CheckBox checkboxBooks = (CheckBox)findViewById(R.id.chkInterestsBooks);
checkboxBooks.setOnCheckedChangeListener(this);
CheckBox checkboxRestaurants = (CheckBox)findViewById(R.id.chkInterestsRestaurants);
checkboxRestaurants.setOnCheckedChangeListener(this);
CheckBox checkboxComputers = (CheckBox)findViewById(R.id.chkInterestsComputers);
checkboxComputers.setOnCheckedChangeListener(this);
CheckBox checkboxTV = (CheckBox)findViewById(R.id.chkInterestsTV);
checkboxTV.setOnCheckedChangeListener(this);
CheckBox checkboxPubs = (CheckBox)findViewById(R.id.chkInterestsPubs);
checkboxPubs.setOnCheckedChangeListener(this);
CheckBox checkboxDancing = (CheckBox)findViewById(R.id.chkInterestsDancing);
checkboxDancing.setOnCheckedChangeListener(this);
//...
}
&安培;你的onCheckedChanged
Map<String, String> states = new HashMap<String, String>();
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
states.put(buttonView.getId(), buttonView..getText().toString());
} else
{
states.remove(buttonView.getId());
}
if(states.size() >= 5)
{
// Your Condition
// selectedStrings will now have all 5 checked CheckBox's Texts
List<String> selectedStrings = new ArrayList<String>(states.values());
}
}
希望它有所帮助。