Java 复选框标签到字符串

Java 复选框标签到字符串,java,android,string,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="2

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

试试这个,可能会对你有所帮助

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
    }
}
int checkBoxCounter=0;
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){

如果(isChecked&&checkBoxCounter尝试一下这个可能会对您有所帮助

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
    }
}
int checkBoxCounter=0;
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){

如果(IsChald&CuffBox计数器你也需要考虑选择和取消选择复选框。在任何给定的点,如果用户选择了其中的5个,则可以继续前进。

大致上,在您的
onCreate

@覆盖
创建时的公共void(Bundle savedInstanceState)
{ 
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_布局);
//...
复选框checkboxMovies=(复选框)findViewById(R.id.chkInterestsMovies);
checkboxMovies.setOnCheckedChangeListener(这个);
复选框checkboxAnimals=(复选框)findViewById(R.id.chkInterestsAnimals);
checkboxAnimals.setOnCheckedChangeListener(这个);
复选框checkboxShopping=(复选框)findViewById(R.id.chkinterestssshopping);
checkboxShopping.setOnCheckedChangeListener(这个);
复选框checkboxBooks=(复选框)findviewbyd(R.id.chkInterestsBooks);
setOnCheckedChangeListener(此);
复选框checkboxRestaurants=(复选框)findViewById(R.id.chkinterestRestaurants);
setOnCheckedChangeListener(这个);
复选框checkboxComputers=(复选框)findViewById(R.id.chkInterestsComputers);
checkboxComputers.setOnCheckedChangeListener(此);
复选框checkboxTV=(复选框)findViewById(R.id.chkinterestv);
checkboxTV.setOnCheckedChangeListener(这个);
复选框checkboxPubs=(复选框)findViewById(R.id.chkInterestsPubs);
checkboxPubs.setOnCheckedChangeListener(这个);
复选框checkboxDancing=(复选框)findViewById(R.id.chkInterestsDancing);
checkboxDancing.setOnCheckedChangeListener(这个);
//...
} 
&您的
onCheckedChanged

Map states=newhashmap();
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查)
{
如果(已检查)
{
states.put(buttonView.getId(),buttonView..getText().toString());
}否则
{
states.remove(buttonView.getId());
}
如果(states.size()>=5)
{
//你的情况
//selectedStrings现在将所有5个复选框的文本都选中
List selectedStrings=newarraylist(states.values());
}
}

希望有帮助。

你也需要考虑选择和取消选择复选框。在任何给定的点,如果用户选择了其中的5个,你可以向前走。

大致上,在您的
onCreate

@覆盖
创建时的公共void(Bundle savedInstanceState)
{ 
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_布局);
//...
复选框checkboxMovies=(复选框)findViewById(R.id.chkInterestsMovies);
checkboxMovies.setOnCheckedChangeListener(这个);
复选框checkboxAnimals=(复选框)findViewById(R.id.chkInterestsAnimals);
checkboxAnimals.setOnCheckedChangeListener(这个);
复选框checkboxShopping=(复选框)findViewById(R.id.chkinterestssshopping);
checkboxShopping.setOnCheckedChangeListener(这个);
复选框checkboxBooks=(复选框)findviewbyd(R.id.chkInterestsBooks);
setOnCheckedChangeListener(此);
复选框checkboxRestaurants=(复选框)findViewById(R.id.chkinterestRestaurants);
setOnCheckedChangeListener(这个);
复选框checkboxComputers=(复选框)findViewById(R.id.chkInterestsComputers);
checkboxComputers.setOnCheckedChangeListener(此);
复选框checkboxTV=(复选框)findViewById(R.id.chkinterestv);
checkboxTV.setOnCheckedChangeListener(这个);
复选框checkboxPubs=(复选框)findViewById(R.id.chkInterestsPubs);
checkboxPubs.setOnCheckedChangeListener(这个);
复选框checkboxDancing=(复选框)findViewById(R.id.chkInterestsDancing);
checkboxDancing.setOnCheckedChangeListener(这个);
//...
} 
&您的
onCheckedChanged

Map states=newhashmap();
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查)
{
如果(已检查)
{
states.put(buttonView.getId(),buttonView..getText().toString());
}否则
{
states.remove(buttonView.getId());
}
如果(states.size()>=5)
{
//你的情况
//selectedStrings现在将所有5个复选框的文本都选中
List selectedStrings=newarraylist(states.values());
}
}

希望有帮助。

我是android新手,我不明白函数如何检查所有复选框更新答案,如果有任何部分不清楚,请随时询问:)@lirazzakaamiri我是android新手,我不明白函数如何检查所有复选框更新答案,如果有任何部分不清楚,请随时询问:)@lirazzakaamir