复选框标签到字符串

时间:2016-02-13 08:16:14

标签: java android string

我有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;" />

2 个答案:

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

希望它有所帮助。