自定义CheckBox

因为需要,需要自己定义个CheckBox。

1.定义checkboxtestxml.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <CheckBox 
        android:id="@+id/checkboxxml_CB1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/mcheckbox"
        android:button="@null"
        
        />
    

</LinearLayout>

2.定义里面的mcheckbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/checkbox_select"></item>
    <item android:state_selected="true" android:drawable="@drawable/checkbox_select"></item>
    <item android:state_checked="false" android:drawable="@drawable/checkbox"></item>

</selector>

3.java代码:



import com.login.login.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class CheckboxTestActivity extends Activity {
	private CheckBox checkboxxml_CB1;
	private Context context = CheckboxTestActivity.this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.checkboxtestxml);
		
		init();
	}

	private void init() {
		checkboxxml_CB1 = (CheckBox) findViewById(R.id.checkboxxml_CB1);
		
		checkboxxml_CB1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					Toast.makeText(context, "选择了", 2).show();
				}else{
					Toast.makeText(context, "没选择", 2).show();
					
				}
			}
		});
	}

}

图片资源直接在百度上面找吧,就2个,一个是选中状态的,一个是没选中的。


注意点:

1.另外必须将android:button设置为@null

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值