Checkbox and radio buttons

Flags have permission to behave like radio buttons. I am developing a quiz application where in the parameters the behavior of the switches and the parameter icon should be like a flag, and is it possible for me to flag when we group the radio buttons?

+5
source share
3 answers

I don’t know if this is the best solution, but you can create a “manager” for your checkboxes and run it when any of them is clicked.

For simplicity, I added a manager to the xml code, but feel free to use setOnClickListener or 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>

You need an ArrayList to iterate, so you can set the status of each flag when they are clicked.

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

    }

, , , , .

+2

, . RadioButton @android:style/Widget.CompoundButton.CheckBox

:

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

radabutton . checkbox .

.

,

RadioButton

- o o

,

CheckBox

?

o o o

-1

All Articles