android开发的CheckBox和RadioButton

来源:互联网 发布:数据采集器 台湾品牌 编辑:程序博客网 时间:2024/05/30 05:00

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)方式引用;


原创粉丝点击