2016-02-13 89 views
1

我有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="&#1505;&#1512;&#1496;&#1497;&#1501;" /> 

<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="&#1495;&#1497;&#1493;&#1514;" /> 

<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="&#1511;&#1504;&#1497;&#1493;&#1514;" /> 

<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="&#1505;&#1508;&#1512;&#1497;&#1501;" /> 

<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="&#1502;&#1505;&#1506;&#1491;&#1493;&#1514;" /> 

<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="&#1502;&#1495;&#1513;&#1489;&#1497;&#1501;" /> 

<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="&#1496;&#1500;&#1493;&#1497;&#1494;&#1497;&#1492;" /> 

<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="&#1508;&#1488;&#1489;&#1497;&#1501;" /> 

<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="&#1512;&#1497;&#1511;&#1493;&#1491;&#1497;&#1501;" /> 

<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="&#1502;&#1493;&#1494;&#1497;&#1511;&#1492;" /> 

<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="&#1489;&#1514;&#1497; &#1511;&#1508;&#1492;" /> 

<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="&#1488;&#1495;&#1512;" /> 

回答

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()); 
    } 
} 

希望它能帮助。

+0

我是新来的android我不明白该功能如何检查所有复选框 –

+0

更新了答案,随时问如果任何部分不清楚:) @LirazShakaAmir –

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 
    } 
}