复选框和单选按钮 [英] Checkbox and radio buttons

查看:78
本文介绍了复选框和单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

复选框是否具有像单选按钮一样的权限.我正在开发一个测验应用程序,其中选项中具有单选按钮的行为,并且选项的图标类似于复选框,我是否可以分组我们将单选按钮分组时的复选框?

Do checkboxes have the permission to behave like radio buttons.I am developing a quiz application where in the options have the behaviour of radio buttons and the icon of the options are to be like the checkbox and is it possible for me to group the checkbox as we group radio buttons?

推荐答案

我不知道这是否是最好的解决方案,但是您可以为复选框创建一个管理器",并在每次单击它们时都运行它.

I don't know if this is the best solution but you can create a "manager" for your checkboxes, and run it whenever any of them gets clicked.

为简单起见,我已在xml代码中添加了管理器,但随时可以使用 setOnClickListener setOnCheckedChangeListener .

For simplicity I've added the manager in the xml code, but feel free to use setOnClickListener, or setOnCheckedChangeListener as well.

<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,以便每当单击其中任何一个复选框时,都可以确定每个复选框的状态.

You need an ArrayList to iterate through, so you can settle the status of each checkbox, whenever any of them gets clicked on.

   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();

    }

在这里,我们有我们的经理,一个迭代器通过整个列表,取消选中所有内容,当到达单击的列表时,进行检查!

And here we have our manager, one iterator to pass trough the whole list, unchecking everything, when it reaches the clicked one, checks!

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);

    }

我还可以在下面找到此解决方案的链接,您可以更改复选框的主题,但是我对主题没有任何经验,因此我无法在这种方法上为您提供任何帮助.

I also could find this other solution linked below, you can change the theme of your checkbox, but I dont have any experience on themes, so I can't help you any further on this approach.

是否可以更改android单选按钮组中的单选按钮图标

这篇关于复选框和单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆