zoukankan      html  css  js  c++  java
  • 一步一步学android控件(之十六)—— CheckBox

    根据使用场景不同,有时候使用系统默认的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 。



  • 相关阅读:
    Jfinal附件上传与重命名
    JFinal-BBS
    jFinal怎样连接sqlserver?
    关于jmeter响应结果用html查看乱码
    jmeter启动报错Error occurred during initialization of VM Could not reserve enough space for object heap errorlevel=1的解决方法
    Genymotion创建下载模拟器的时候出现Unable to create Genymotion virtual devices:Connection timeout错误
    Selenium IDE和Selenium RC的安装
    python+eclipse环境搭建
    第一个jemter测试脚本
    ulipad源码包配置环境及安装
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3435789.html
Copyright © 2011-2022 走看看