android開發的CheckBox和RadioButton

1、改變選擇框:以RadioButton爲例

<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rb_question"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:textColor="@color/c_22"
    android:button="@null"
    android:drawableLeft="@drawable/radio_question_state"
    android:drawablePadding="@dimen/d16px"
    android:textSize="@dimen/s32px"
    android:paddingBottom="@dimen/d30px"
    >

</RadioButton>

在drawable中新建radio_question_state

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_checked="false"
        android:drawable="@mipmap/radio_answer_normal"/>
    <item
        android:state_checked="true"
        android:drawable="@mipmap/radio_answer_select"/>
</selector>
2、選中狀態判斷

CheckBox:

cb.setId(i);

                cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

                        listAnswer.getAnswer().get(compoundButton.getId()).setSelect(isChecked);
                        
                    }
                });
RadioButton:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
                listAnswer.getAnswer().get(checkId).setSelect(true);
                if(ActivityUtils.isActivityExist(QuestionActivity.class)){
                    ActivityUtils.getActivity(QuestionActivity.class).setQuestionListBean(number,listAnswer);
                }

            }
        });

注:儘量不要在自定義控件中設置改變drawableLeft,會出現些奇葩的問題。

最好用CheckBox cb = UIUtils.inflate(context,R.layout.check_question).findViewById(R.id.cb_answer)方式引用;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章