Android 巧用CheckBox

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、效果图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值