How I can have behavior RadioGroup SingleChoice for 2 or more checkbox on android?(at here for 2 checkbox ,specially)

☆樱花仙子☆ 提交于 2019-12-11 16:47:23

问题


Hello stackoverflow friends. I am new by android and I have a simple question that bothers me! I have 2 CheckBox(CheckBoxAutomat and CheckBoxManual for example) . I want have like RadioButton group of type SingleChoice behavior for my checkBoxes. I write this code but it has wrong result for me. How I can have behavior RadioGroup SingleChoice for 2 or more checkbox(at here for 2 checkbox ,specially)? Code:

               CheckBoxAutomat.setOnCheckedChangeListener(new OnCheckedChangeListener() 
               {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
                {
                    // TODO Auto-generated method stub
                        if(CheckBoxManual.isChecked())
                    { 
                            CheckBoxManual.setChecked(false);



                    }

                }
             });
               //..............................
               CheckBoxManual.setOnCheckedChangeListener(new OnCheckedChangeListener() 
               {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
                {
                    // TODO Auto-generated method stub
                    if(CheckBoxAutomat.isChecked())
                    { 
                        CheckBoxAutomat.setChecked(false);


                    }

                }
            });

回答1:


I asked this question because I didnt like the defualt style of checkoboxes,while I needed radibutton surely.So I put radioGroup on my app and then change android:button with a checkbox_selector(with my favorite pictures ) which I created it. a cb_selector like this :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/checked2" />
    <item android:state_checked="false" android:drawable="@drawable/unchecked2" />
</selector>

this must write in a xml file in drawable forlder. then i write @drawable/cb_selector in Button property for each radiobutton in my radioGroup.



来源:https://stackoverflow.com/questions/21063942/how-i-can-have-behavior-radiogroup-singlechoice-for-2-or-more-checkbox-on-androi

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