当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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

}

}

});

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: