您的位置:首页 > 移动开发 > Android开发

安卓自定义View(第一篇)

2014-04-17 14:36 253 查看
【Android常用基本控件】

  1.1Android控件基本介绍

  Android本身提供了很多控件,比如我们常用的有:文本控件(TextView和EditText)、按钮控件(Button和ImageButton)、状态开关按钮(ToggleButton)、单选复选按钮(RadioButton和RadioGroup)、单选按钮和复选按钮(CheckBox和RadioButton)、图片控件(ImageView)、时钟控件(AnalogClock和DigitalClock)、进度条(ProgressBar)和日期与时间选择控件(DatePicker和TimePicker)等。但是这些控件并不能满足我们所有的要求。有的时候我们必须要自己定义控件来满足我们的要求。
  

     1.1.1文本控件(TextView和EditText)

  在android中文本控件分为:TextView控件和EditText控件

  TextView控件

  TextView控件继承自View类。TextView控件的功能是向用户显示文本内容,同时可选择性让用户编辑文本。其中TextView不允许编辑。

  EditText控件

  EditText控件继承自TextView。EditText与TextView最大的不同是EditText是可以编辑的。

  1.1.2按钮控件(Button和ImageButton)

  Android中的按钮可以分为:Button和ImageButton两种控件

  Button控件

  Button控件继承自TextView类,Button的用法比较简单,主要是为Button控件设置View.OnClickListener.监听器,并在监听器的实现代码中编写按钮按下事件的处理代码。

  ImageButton控件

  ImageButton继承自ImageView。ImageButton与Button最大的区别是ImageButton没有text属性,既按钮中将显示图片而不是文本。ImageButton控件中设置显示图片可以通过android:src属性,也可以通过setImageResurce(int)方法来实现。

  1.1.3状态开关按钮(ToggleButton)

  ToggleButton控件是继承自Button。ToggleButton的状态只能是选中和未选中,并且需要为不同的状态设置不同的显示文本。除了继承自父类的一些属性和方法之外,

  ToggleButton也具有一些自己的ToggleButton属性。

  1.1.4单选按钮和复选按钮(CheckBox和RadioButton)

  CheckBox和RadioButton都只有选中和未选中两种状态,可以通过android:check属性来设置。不同的是RadioButton是单选按钮,需要编制到一个RadioGroup中,同一时刻一个RadioGroup中只能有一个按钮处于选中状态。

  CheckBox和RadioButton都是继承自CompoundButton中继承了一些成员

  1.1.5图片控件(ImageView)

  ImageView控件负责显示图片,其图片来源既可以是资源文件的id,也可以是Drawable对象或Bitmap对象,还可以是ContentProvider的Uri。

  1.1.6时钟控件(AnalogClock和DigitalClock)
  时钟控件包括AnalogClock和DigitalClock控件,所不同的是AnalogClock继承自View,而DigeitalClock继承自TextView。并且AnalogClock控件显示模拟时钟,只显示时针和分针,而DigetalClock显示数字时钟,可精确到秒。时钟控件比较简单,只需要在布局文件中声明控件即可。

  1.1.7日期与时间选择控件(DatePicker和TimePicker)

  DatePicker继承自FrameLayout类,日期选择控件的主要功能是向用户提供包含年、月、日的日期数据并允许用户对其进行选择。如果要捕获用户修改日期选择控件中数据的事件,需要为DatePicker添加onDateChangedListener监听器。TimePicker同样继承自FrameLayout类。时间选择控件向用户显示一天中的时间(可以为24小时制,可以为AM/PM制),并允许用户进行选择。如果要捕获用户修改时间数据的事件,便需要为TimePicker添加OnTimeChangedListener监听器。

 1.2继承已有控件实现自定义控件

  通过对android本身提供的控件的代码进行研究,android中控件都是继承view类来实现,通过重写ondraw方法来绘制我们所需要的控件。通过这个我们得到两点提示,第一,我们可以在已有的控件的基础上,通过重写相关方法来实现我们的需求。第二就是继承view类或viewgroup类,来绘制我们所需要的控件。一般来讲,通过继承已有的控件来自定义控件要简单一点。下面分别对上述情况举例说明:

  1.2.1控件之RadioButton

  1、举一个最简单的例子说明,比如现在的RadioButton按钮只能存在一个text,如果我们想存储key-value对应的键值对,那么我们就需要自定义一个控件。这时定义的控件仅仅比RadioButton多了一个存储key的控件。实现如下:首先在开始前,我们需要检查在

values目录下是否有attrs.xml,如果没有则创建。下面把创建的代码贴出来,如下:
  <?xmlversion="1.0"encoding="utf-8"?>
  <resources>
  <declare-styleablename="MRadioButton">
  <attrname="value"format="string"/>
  </declare-styleable>
  </resources>


  其中:外层name表示该控件的名称,内层name表示属性的名称,format表示类型。然后在创建MRadioButton类,该类继承RadioButton。代码如下:
package com.jabony.customerview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

public class MRadioButton extends RadioButton implements
OnCheckedChangeListener {
private String mValue;

public MRadioButton(Context context) {
super(context);
}

public MRadioButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public String getValue() {
return this.mValue;
}

public void setValue(String value) {
this.mValue = value;
}

public MRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MRadioButton);
this.mValue = a.getString(R.styleable.MRadioButton_value);
invalidate();
a.recycle();
setOnCheckedChangeListener(this);
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
}


  通过mValue就可以对该控件设置key了,this.mValue=a.getString(R.styleable.MRadioButton_value);可以把在layout里面定义的key给取出来。下面在看

  layout中的代码:
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:key="http://schemas.android.com/apk/res/com.jabony.customerview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <com.jabony.customerview.MRadioButton
        android:id="@+id/mRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textSize="24sp"
        key:value="true" >
    </com.jabony.customerview.MRadioButton>
</LinearLayout>


  其中:xmlns:key="http://schemas.android.com/apk/res/com.jabony.customerview"为定义命名空间路径,在这里定义完后,就可以对value进行赋值了。com.jabony.customerview为该控件的路径。到此为止已经简单的把如何自定义一个控件的流程讲述一遍。当然这仅仅是最简单的一个例子。如果要实现一些复杂效果,不是仅仅在构造函数中可以完成,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 控件 自定义