一步一步学android控件,一步一步学android控件(之十六)—— CheckBox

根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式。今天主要学习如何自定义CheckBox样式。在CheckBox状态改变时有时需要做一些额外的工作,可以在OnCheckedChangeListener做这些工作。所以今天的内容如下:

1、系统默认样式

2、自定义checkBox样式

3、使用OnCheckedChangeListener监听器监听CheckBox的选中状态。

先看一下效果图:

21873c20584f235c565e3ba5e2d70a4f.png             

47e0a295bb49d620e6b22b9fa51b0136.png

图一                                                          图二

图一为默认样式和自定义样式,当checkBox的checked状态发生改变时弹出一个Toast提示框,如图二所示。

系统默认样式没啥可说的,下面主要讲讲如何自定义自己的样式

首先准备两张图片:

fd0218624a2744fc5e735ad783e7f559.png      check_box_normal.jpg

e75afd3ecbbe212ae6dc9d37cfd64a21.png      check_box_checked.jpg

1、定义selector  , check_box_style.xml:

2、定义CheckBox的样式:

@drawable/check_box_style

@color/text_view_stroke

3、在checkBox中使用该样式:

android:id="@+id/show_checkbox_style_customer"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/customer_checkbox_style"

style="@style/check_box_theme"/>

4、监听CheckBox状态变化

mCheckBoxCustomer.setOnCheckedChangeListener(this);

选中状态改变,doSometing

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

Toast.makeText(this, "check box state changed !..", Toast.LENGTH_LONG).show();

}

下面是详细代码:

1、布局文件widget_checkbox_layout.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center_horizontal"

android:orientation="vertical" >

android:id="@+id/show_checkbox_style"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/default_checkbox_style" />

android:id="@+id/show_checkbox_style_customer"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/customer_checkbox_style"

style="@style/check_box_theme"/>

2、activity——WidgetCheckBoxActivity.java

package com.xy.zt.selfdefinewieget;

import android.app.Activity;

import android.os.Bundle;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.Toast;

import android.widget.CompoundButton.OnCheckedChangeListener;

public class WidgetCheckBoxActivity extends Activity implements OnCheckedChangeListener{

CheckBox mCheckBoxDefault ;

CheckBox mCheckBoxCustomer ;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.widget_checkbox_layout);

init();

}

private void init(){

mCheckBoxDefault = (CheckBox) findViewById(R.id.show_checkbox_style);

mCheckBoxDefault.setOnCheckedChangeListener(this);

mCheckBoxCustomer = (CheckBox) findViewById(R.id.show_checkbox_style_customer);

mCheckBoxCustomer.setOnCheckedChangeListener(this);

}

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

Toast.makeText(this, "check box state changed !..", Toast.LENGTH_LONG).show();

}

}

3、在ViewData.java和WidgetsAdapter中分别添加如下内容:

ViewData.java

public static final int CHECK_BOX_ID = DEGITAL_CLOCK_ID + 1;

public static final String CHECK_BOX_NAME = "CheckBox";

private static final ViewData mCheckBox = new ViewData(CHECK_BOX_NAME,

CHECK_BOX_ID);

View_Datas.add(mCheckBox);

WidgetsAdapter的handleItemClicked方法:

case ViewData.CHECK_BOX_ID:

intent.setClass(mContext, WidgetCheckBoxActivity.class);

mContext.startActivity(intent);

break;

CheckBox控件就学到这里,下一篇文章学习 RadioButton & RadioGroup 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值