Android自定义控件的好处

来源:互联网 发布:开源三网合一源码 编辑:程序博客网 时间:2024/05/01 05:30
  • 什么是Andorid自定义视图
    对于Android应用开发者而言,Android 中自带的控件并不陌生,常用的单独控件有TextView(文本框)、EditText(编辑框)、Button( 按钮)、ImageView(图片视图)等,组合控件(即用来摆放多个单独控件的容器)常见的有LinearLayout(线性布局)、RelativeLayout(相对布局)、TableLayout(表格布局)、FrameLayout(帧布局)等;在大多数情况下,Android 为开发者提供的这些控件已经足够我们使用。但在某些情况下,如项目中需要定制化视图,这个时候就不得不使用到自定义视图方面的知识了。
    什么是自定义控件?自定义控件是基于项目开发中定制化视图的需要,或为了提高复用性而基于既有控件扩展封装的控件。
  • 为什么要使用Android自定义控件
    对于没有怎么使用自定义控件的程序员来说,多少会有点抵触,因为他们会觉得Android系统已为程序员提供了大量控件,足够开发使用,而且网上开源的控件也足够丰富,何需自己去自定义。不过,授之以鱼,不如授之以渔。了解自定义原理,是android 开发进阶必经的一步。了解自定义的相关知识,可以基于系统自带的控件扩展封装符合项目所需的控件,如下边将举例的基于Button进行封装的获取验证码的倒计时控件。虽然网上开源的自定义控件也不少,但有时候没法完全满足项目开发的需要,这个时候若是了解了自定义知识,就可以更加方便的修改这些开源自定义控件,为己所用。

  • 如何使用Android自定义控件
    自定义控件包括自定义View 和自定义ViewGroup.
    先总结下自定义View的步骤:
    1、自定义View的属性
    2、在View的构造方法中获得我们自定义的属性
    3、重写onMeasure (简单的应用中有时不需要重写)
    4、重写onDraw(基于Android控制扩展的有时不需要重写)

    自定义ViewGroup 有时还需要重写onLayout方法。
    当然这里只是简单介绍一下,因为本文的重点是让读者理解自定义的好处,相关细节原理可以参考以下博客
    Android LayoutInflater原理分析,带你一步步深入了解View(一)
    Android视图绘制流程完全解析,带你一步步深入了解View(二)

  • 举个简单实用自定义例子
    下面举一个基于Button进行封装的获取验证码的倒计时控件

1、首先自定义View的属性,需要在values目录下建一个attr文件,用于定义自定义控件的属性

<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="CountDownButton">        <!-- 倒计时秒数 -->        <attr name="seconds" format="integer" />        <!-- 倒计时结束后显示的文字 -->        <attr name="afterText" format="string" />        <!-- 倒计时显示的额外文字,如"秒后重新获取" -->        <attr name="additionalText" format="string" />    </declare-styleable></resources>

2、在View的构造方法中获得我们自定义的属性

public class CountDownButton extends Button {    private int seconds;    private String afterText;    private String addionalText;    public CountDownButton(Context context) {        this(context,null,0);    }    public CountDownButton(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public CountDownButton(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        //在View的构造方法中获得我们自定义的属性        TypedArray a=context.getResources().obtainAttributes(attrs, R.styleable.CountDownButton);        seconds=a.getInteger(R.styleable.CountDownButton_seconds,60);        afterText=a.getString(R.styleable.CountDownButton_afterText);        addionalText=a.getString(R.styleable.CountDownButton_additionalText);        a.recycle();    }}

3、为自定义封装倒计时逻辑

public void countDown(){    //CountDownTimer(long millisInFuture, long countDownInterval)    //millisInFuture 将要倒计时的总时间,单位是毫秒    //countDownInterval 每次倒计时的时间间隔     new CountDownTimer(seconds*1000, 1000) {            @Override            public void onTick(long millisUntilFinished) {                setFocusable(false);                setClickable(false);                setEnabled(false);                setText(""+millisUntilFinished/1000+addionalText);            }            @Override            public void onFinish() {                setFocusable(true);                setClickable(true);                setEnabled(true);                if(!TextUtils.isEmpty(afterText))setText(afterText);            }        }.start();    }

4、在xml布局中使用自定义控件,以下是layout_main中的内容.需要注意的是,布局中需要引用

xmlns:标签="http://schemas.android.com/apk/res/+应用包名"

以下标签为 app,应用包名为com.customview

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res/com.customview"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <com.customview.CountDownButton        android:id="@+id/cdbtn_vcode"        android:layout_centerInParent="true"        android:layout_width="300dp"        android:layout_height="100dp"        android:gravity="center"        app:seconds="60"        app:additionalText="秒后重新获取"        app:afterText="点击重发"        android:background="@drawable/selector_back"        android:textColor="@color/selector_text"        android:text="获取验证码" /></RelativeLayout>

附上倒计时按钮的文字选择器和背景选择器

//文字选择器 selector_text<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_enabled="false" android:color="#7d7d7d"/>    <item android:state_enabled="true" android:color="#fffffe"/></selector>
//背景选择器<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_enabled="true" >        <shape >            <solid android:color="#cb0821" />              <corners android:radius="8dip" />              </shape>    </item>    <item       android:state_enabled="false">        <shape >            <solid android:color="#bbbbbb" />              <corners android:radius="8dip" />              </shape>    </item></selector>

5、在MainActivity中,引用自定义倒计时控件

public class MainActivity extends Activity {    private CountDownButton cdbtn_vcode;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        cdbtn_vcode = (CountDownButton) findViewById(R.id.cdbtn_vcode);        cdbtn_vcode.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                cdbtn_vcode.countDown();                //*************************                //联网获取验证码                //*************************            }        });    }}

点击前、倒计时中、倒计时结束后的效果图展示如下:
这里写图片描述 这里写图片描述 这里写图片描述

  • 小结
    以上举的例子比较简单,只是基于Button 控件结合倒计时逻辑进行的封装扩展,并没有重写onMeasure 和 onDraw 方法。但自定义的好处已经显示出来了。
    1、复用性提高了,以后可随时复用倒计时控件。
    2、客户端代码更简洁,只需要调用倒计时方法countDown();
    3、配置更灵活,在xml中配置倒计时按钮的属性

当然,自定义的知识肯定不止这些,更多的自定义知识还待读者自己去学习补充。

例子源码地址:http://download.csdn.net/download/wuguiye/9086335

0 0
原创粉丝点击