Android布局控件CheckBox应用
1、布局文件
<LinearLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip"
android:layout_marginTop="20dp"
android:divider="@drawable/record_period_hadsex_seperator_long"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:showDividers="middle" >
<CheckBox
android:id="@+id/check1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/check1"
android:button="@null" />
<CheckBox
android:id="@+id/check2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/check2"
android:button="@null" />
<CheckBox
android:id="@+id/check3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/check3"
android:button="@null" />
<CheckBox
android:id="@+id/check4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/check4"
android:button="@null" />
</LinearLayout>
2、CheckBox中引用的背景资源文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/check1_over" android:state_checked="true"/>
<item android:drawable="@drawable/check1_nor"/>
</selector>
3、Java实现代码
CheckBox check1, check2, check3, check4;
protected void onCreate(Bundle savedInstanceState) {
check1 = (CheckBox) this.findViewById(R.id.check1);
check2 = (CheckBox) this.findViewById(R.id.check2);
check3 = (CheckBox) this.findViewById(R.id.check3);
check4 = (CheckBox) this.findViewById(R.id.check4);
check1.setOnCheckedChangeListener(checkbuttondo);
check2.setOnCheckedChangeListener(checkbuttondo);
check3.setOnCheckedChangeListener(checkbuttondo);
check4.setOnCheckedChangeListener(checkbuttondo);
}
private CheckBox.OnCheckedChangeListener checkbuttondo = new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// isChecked 是否被选中状态 选中为true 再次点消除为false,状态无默认值
switch (buttonView.getId()) {
case R.id.check1:
break;
case R.id.check2:
break;
case R.id.check3:
break;
case R.id.check4:
break;
}
}
};
4、效果图