【发布时间】:2011-10-18 03:51:10
【问题描述】:
复选框是否有权像单选按钮一样工作。我正在开发一个测验应用程序,其中的选项具有单选按钮的行为,并且选项的图标类似于复选框,我可以分组吗我们分组单选按钮时的复选框?
【问题讨论】:
标签: android-xml
复选框是否有权像单选按钮一样工作。我正在开发一个测验应用程序,其中的选项具有单选按钮的行为,并且选项的图标类似于复选框,我可以分组吗我们分组单选按钮时的复选框?
【问题讨论】:
标签: android-xml
如果您想要看起来像复选框的单选按钮。设置 RadioButton 的样式为@android:style/Widget.CompoundButton.CheckBox
例如:
<RadioButton style="@android:style/Widget.CompoundButton.CheckBox" />
【讨论】:
我不知道这是否是最好的解决方案,但您可以为您的复选框创建一个“管理器”,并在其中任何一个被点击时运行它。
为简单起见,我在 xml 代码中添加了管理器,但您也可以随意使用 setOnClickListener 或 setOnCheckedChangeListener。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox1"
android:onClick="cbgroupmanager"
/>
...
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox5"
android:onClick="cbgroupmanager"/>
</LinearLayout>
您需要一个 ArrayList 进行迭代,这样您就可以确定每个复选框的状态,只要其中任何一个被点击。
public class Q6910875 extends Activity
ArrayList<CheckBox> cb = new ArrayList<CheckBox>();
int CheckBoxNum = 5; //number of checkboxes
Iterator<CheckBox> itr ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cb.add((CheckBox) findViewById(R.id.checkBox1));
cb.add((CheckBox) findViewById(R.id.checkBox2));
cb.add((CheckBox) findViewById(R.id.checkBox3));
cb.add((CheckBox) findViewById(R.id.checkBox4));
cb.add((CheckBox) findViewById(R.id.checkBox5));
itr = cb.iterator();
}
这里我们有我们的经理,一个遍历整个列表的迭代器,取消选中所有内容,当它到达点击的那个时,检查!
public void cbgroupmanager(View v) {
CheckBox cbaux;
while(itr.hasNext()) {
cbaux = (CheckBox) itr.next(); // we need this because it returns a Object, and we need the setChecked, which is a CheckBox method.
Log.d("soa", "click");
if (cbaux.equals(v)) //if its the one clicked, mark it as checked!
cbaux.setChecked(true);
else
cbaux.setChecked(false);
}
我也可以找到下面链接的其他解决方案,您可以更改复选框的主题,但我对主题没有任何经验,因此无法进一步帮助您。
Is it possible to change the radio button icon in an android radio button group
【讨论】:
在radiabutton中,我们可以做出唯一的决定。但是 复选框,我们可以有多个选项。
根据我们选择这些控件的用途。
例如,
性别 - o 男 o 女
这里我们只能选择一个选项
例如,
您感兴趣的游戏?
板球 o 足球 o 谷球 o 曲棍球
【讨论】: