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

1、系统默认样式

2、自定义checkBox样式

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

先看一下效果图:

             

图一                                                          图二

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

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

首先准备两张图片:

      check_box_normal.jpg

      check_box_checked.jpg

1、定义selector  , check_box_style.xml:

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

2、定义CheckBox的样式:

<style name="check_box_theme" ><!-- parent="@android:style/Widget.CompoundButton.CheckBox" -->
<item name="android:button">@drawable/check_box_style</item>
<item name="android:textColor">@color/text_view_stroke</item>
</style>

3、在checkBox中使用该样式:

 <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

<?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:gravity="center_horizontal"
android:orientation="vertical" > <CheckBox
android:id="@+id/show_checkbox_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_checkbox_style" />
<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"/> </LinearLayout>

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;

对第三点有不清楚的地方参见文章一步一步学android控件(之一) —— 开始篇

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

一步一步学android控件(之十六)—— CheckBox的更多相关文章

  1. 一步一步学android控件(之十五) —— DegitalClock & AnalogClock

    原本计划DigitalClock和AnalogClock单独各一篇来写,但是想想,两个控件的作用都一样,就和在一起写一篇了. DegitalClock和AnalogClock控件主要用于显示当前时间信 ...

  2. 一步一步学android控件(之六) —— MultiAutoCompleteTextView

    今天学习的控件是MultiAutoCompleteTextView . 提到MultiAutoCompleteTextView 我们就自然而然地想到AutoCompleteTextView ,就想知道 ...

  3. 一步一步学android控件(之二十五)—— SeekBar

    SeekBar扩展自ProgressBar——在ProgressBar的基础上添加了一个用户可以拖拽的thum. SeekBar.OnSeekBarChangeListener是接收SeekBar进度 ...

  4. python selenium 处理时间日期控件(十六)

    测试过程中经常遇到时间控件,需要我们来选择日期,一般处理时间控件通过层级定位来操作或者通过调用js来实现. 1.首先我们看一下如何通过层级定位来操作时间控件. 通过示例图可以看到,日期控件是无法输入日 ...

  5. Android 控件架构及View、ViewGroup的测量

    附录:示例代码地址 控件在Android开发的过程中是必不可少的,无论是我们在使用系统控件还是自定义的控件.下面我们将讲解一下Android的控件架构,以及如何实现自定义控件. 1.Android控件 ...

  6. Android控件TextView的实现原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8636153 在前面一个系列的文章中,我们以窗口 ...

  7. Android群英传笔记——第三章:Android控件架构与自定义控件讲解

    Android群英传笔记--第三章:Android控件架构与自定义控件讲解 真的很久没有更新博客了,三四天了吧,搬家干嘛的,心累,事件又很紧,抽时间把第三章大致的看完了,当然,我还是有一点View的基 ...

  8. Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现

    Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现 2015-03-10 22:38 28419人阅读 评论(17) 收藏 举报  分类: Android ...

  9. [Android Pro] android控件ListView顶部或者底部也显示分割线

    reference to  :  http://blog.csdn.net/lovexieyuan520/article/details/50846569 在默认的Android控件ListView在 ...

随机推荐

  1. 再写FFT模板

    没什么好说的,今天又考了FFT(虽然不用FFT也能过)但是确实有忘了怎么写FFT了,于是乎只有重新写一遍FFT模板练一下手了.第一部分普通FFT,第二部分数论FFT,记一下模数2^23*7*17+1 ...

  2. [XJOI NOI2015模拟题13] C 白黑树 【线段树合并】

    题目链接:XJOI - NOI2015-13 - C 题目分析 使用神奇的线段树合并在 O(nlogn) 的时间复杂度内解决这道题目. 对树上的每个点都建立一棵线段树,key是时间(即第几次操作),动 ...

  3. Layer 1: Single Objects

    Layer 1: 单一对象 粗略的说, 在javascript中所有对象都是maps的键值对. 键值对的实体在对象中称为属性( property).属性的key经常为 string类型,而他的valu ...

  4. NSMutableArray,NSMutableDictionary的内存管问题

    今天做项目遇到一个问题,在一个类中定义了一个可变数组,使用的是copy的内存管理策略 当往数组中添加包装好的基本数据的时候,程序直接崩溃了.解决方法:把copy换成strong就不会崩溃了; 后来做了 ...

  5. 在ubuntu下关闭笔记本触摸板

    http://www.cnblogs.com/icejoywoo/archive/2011/04/14/2016318.html 原文地址:http://forum.ubuntu.org.cn/vie ...

  6. JAVA三大框架的各自作用

    http://christhb.blog.163.com/blog/static/98982492011727114936239/ 一.Spring Spring是一个解决了许多在J2EE开发中常见的 ...

  7. ANDROID_MARS学习笔记_S02_010_Animation_动画效果

    一.流程 1.把要实现动画的一系列图片复制到res/drawable文件夹 2.在此文件新建一个xml文件用来组织图片 3.在mainactivity中用imageView.setBackground ...

  8. 163. Missing Ranges

    题目: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return it ...

  9. Android 在AlertDialog里添加布局控件

    android里很多时候需要在弹出的AlertDialog里有自己的控件,填写信息,比如弹出一个登陆对话框 那么首先你就要创建这么一个布局的inputphonenum.xml文件了 <?xml ...

  10. wzplayer for ios 针对(mms)优化版本V1.0

    wzplayer for ios针对mms优化版本发布. 1.支持mms,http,rtmp,rtsp等协议 2.支持全格式 下载地址:http://www.coolradio.cn/WzPlayer ...