gpt4 book ai didi

java - 复选框标签到字符串

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:03 26 4
gpt4 key购买 nike

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

最佳答案

您还需要考虑选择和取消选择复选框。在任何给定点,如果用户在所有选项中选择了 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());
}
}

希望有帮助。

关于java - 复选框标签到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35377653/

26 4 0