How to show pre checked checkboxes in android

前端 未结 3 1535
花落未央
花落未央 2021-01-08 00:15

I have a problem
How do I show prechecked checkboxes in my android application.
Suppose there are 4 check boxes and I want to show 2 of them checked from the beginni

相关标签:
3条回答
  • 2021-01-08 00:42

    You can either use xml property

    <CheckBox
        android:id="@+id/cb1"
        .... 
        android:checked="true"
    />
    

    or set in your code like

    boolean isChecked = ...;
    CheckBox cb1 = (CheckBox)findViewById(R.id.cb1);
    cb1.setChecked(isChecked);
    
    0 讨论(0)
  • 2021-01-08 00:57

    Just set your value in

    checkBox.setTag("zero") or

    checkBox.setTag("one")
    

    and check like this ...

    String str = (String) checkBox.getTag();
    
    if(str.equals("zero") || str.equals("one") ){
    checkBox.setChecked(true);
    }
    
    0 讨论(0)
  • 2021-01-08 01:04

    Vladimir's answer didn't work for me. Instead use this:

    <CheckBox
    ...
    android:state_checked="true" />
    
    0 讨论(0)
提交回复
热议问题