CheckBox复选框的
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你最大的梦想是:"
android:textColor="#000"
android:textSize="20dp" />
<CheckBox
android:id="@+id/cb_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="暴富"
android:button="@drawable/bg_checkbox"
android:textSize="20sp" />
<CheckBox
android:id="@+id/cb_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="暴瘦"
android:button="@drawable/bg_checkbox"
android:textSize="20sp" />
<CheckBox
android:id="@+id/cb_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="长高"
android:button="@drawable/bg_checkbox"
android:textSize="20sp" />
<CheckBox
android:id="@+id/cb_8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="变白"
android:button="@drawable/bg_checkbox"
android:textSize="20sp" />
在java文件中 编写如下
先声明控件
mCb5=(CheckBox)findViewById(R.id.cb_5);
mCb6=(CheckBox)findViewById(R.id.cb_6);
mCb7=(CheckBox)findViewById(R.id.cb_7);
mCb8=(CheckBox)findViewById(R.id.cb_8);
mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override//设置状态发生变化的监听事件
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(CheckBoxActivity.this,b?"哈哈有钱了":"想多了",Toast.LENGTH_SHORT).show();
}
});
mCb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override//设置状态发生变化的监听事件
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(CheckBoxActivity.this,b?"你又瘦了":"别吃了胖死得了",Toast.LENGTH_SHORT).show();
}
});
mCb7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override//设置状态发生变化的监听事件
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(CheckBoxActivity.this,b?"看我一米八的大长腿":"呵呵,小矮子",Toast.LENGTH_SHORT).show();
}
});
mCb8.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override//设置状态发生变化的监听事件
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(CheckBoxActivity.this,b?"一白遮白丑":"怕是白痴吧",Toast.LENGTH_SHORT).show();
}
});
在当然了文件之下新建Drawable Resource file文件,命名为bg_checkbox,根布局为selector 的这样的一个背景选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false" <--不选中时-->
android:drawable="@drawable/lala_false"<--自定义添加的图片-->
/>
<item
android:state_checked="true" <--选中时-->
android:drawable="@drawable/haha_true"<--自定义添加的图片--> />
</selector>