Checkbox and radio buttons

て烟熏妆下的殇ゞ 提交于 2020-01-03 09:08:15

问题


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?


回答1:


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.

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>

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.

Is it possible to change the radio button icon in an android radio button group




回答2:


If you want Radio Buttons which look like Check boxes. Set Style of RadioButton as @android:style/Widget.CompoundButton.CheckBox

e.g:

<RadioButton style="@android:style/Widget.CompoundButton.CheckBox" />



回答3:


In radiabutton we can have the only one decision.But checkbox we can have mulitiple options.

based on the usage we choose these controls.

for example,

RadioButton

Gender - o Male o Female

here we can select only one option

for example,

CheckBox

your Interested Games?

o cricket o football o valleyball o hockey



来源:https://stackoverflow.com/questions/6910875/checkbox-and-radio-buttons

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!