如何检查一排按钮中是否至少有一个 ToggleButton 被选中?
我正在致力于创建一个应用程序,该应用程序除其他功能外还具有集成的GAD 测试功能(用于计算和测量用户压力水平的自测)。它看起来是这样的:
它由一个表格组成,其中包含多行切换按钮。作为示例,这是其中 1 个按钮的代码:
<ToggleButton
android:id="@+id/row1_btn4"
android:layout_width="200px"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/button_border"
android:gravity="center"
android:paddingStart="10px"
android:paddingEnd="10px"
android:scaleX="0.5"
android:scaleY="0.65"
android:textColor="@color/white"
android:textOff=" "
android:textOn="✓"
android:textSize="28sp" />
这是检查按钮是否被选中的代码:
row1_btn4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
gadpoints += 3;
((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
} else if (!isChecked) {
gadpoints -= 3;
((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
} else {
gadpoints += 0;
((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
}
}
});
一切都按预期工作,如果 ToggleButton 被选中,则用户被授予给定的积分。但是,我想实现两件事:
a)使其只能检查每行中的1个按钮,并防止用户检查同一行中的另一个按钮(如果他/她已经)检查1
b)检查在一行按钮中是否没有一个被检查,如果是,则通知用户
我想不出可行的解决方案,因为我本质上将检查按钮是否未被检查,但话又说回来,其中一些是不应该受到检查的。有什么想法吗?
I'm working on creating an application, which amongst other features has an integrated GAD Test functionality (self test to calculate and measure the user's stress level). This is how it looks:
It is consisted of a Table, with multiple rows of ToggleButtons. This is the code for 1 of the buttons, as an example:
<ToggleButton
android:id="@+id/row1_btn4"
android:layout_width="200px"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/button_border"
android:gravity="center"
android:paddingStart="10px"
android:paddingEnd="10px"
android:scaleX="0.5"
android:scaleY="0.65"
android:textColor="@color/white"
android:textOff=" "
android:textOn="✓"
android:textSize="28sp" />
and this is the code to check whether a button is checked or not:
row1_btn4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
gadpoints += 3;
((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
} else if (!isChecked) {
gadpoints -= 3;
((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
} else {
gadpoints += 0;
((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
}
}
});
Everything is working as it should, if a ToggleButton is checked, the user is awarded the given points. However, I would like to implement 2 things:
a) Make it so that only 1 button from each row can be checked, and prevent the user from checking another one from the same row if he / she already checked 1
b) Check whether in a row of buttons none of them has been checked, and if so, notify the user
I cannot think of a feasible solution to this, because I will be essentially checking if a button hasn't been checked, but then again, some of them are meant to be unchecked. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
RadioGroup
来管理每行仅选择一个条目。要在未选择任何内容的情况下启动RadioGroup
,您可以对其调用clearCheck()
。要确保在单击“提交”时在所有行中进行选择,您可以在每个组/行上调用 getCheckedRadioButtonId(),如果未选择任何内容,它将返回 -1。
如果您想自定义单选按钮的外观,有很多关于如何执行此操作的示例 这里。
下面是
onCreate
中的示例:以及随之而来的 XML
You can manage having only one entry selected per row using a
RadioGroup
. To start theRadioGroup
with nothing selected you can callclearCheck()
on it.To make sure that a selection was made in all rows when they click "Submit" you can call
getCheckedRadioButtonId()
on each group/row, it will return -1 if nothing was selected.If you want to customize what the radio buttons look like there are lots of examples of how to do that here.
Here is an example of what that would look like in
onCreate
:and the XML that goes along with it
不要使用切换按钮,而是为每行使用 Radio Group 。
然后,您可以为单选按钮设计一个自定义可绘制对象,使它们看起来像您当前拥有的那样。
使用此创建自定义按钮
您可以从 android studio 中的资源管理器获取 Box 和 Check 可绘制对象。
Instead of using toggle buttons, use a Radio Group for each row.
Then you can design a custom drawable for Radio Buttons to make them look like what you currently have.
Create custom button using this
You can get Box and Check drawable from Resource Manager in android studio.