开发者

Checkbox and radio buttons

Do checkboxes have the permission to b开发者_高级运维ehave 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?


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" />


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜