问题
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