Chinaunix

标题: Android自定义控件系列之基础篇(烟台杰瑞教育原创) [打印本页]

作者: qdjianghao    时间: 2015-03-25 14:31
标题: Android自定义控件系列之基础篇(烟台杰瑞教育原创)
本帖最后由 qdjianghao 于 2015-03-25 14:35 编辑

一、概述

  在android开发中很多UI控件往往需要进行定制以满足应用的需要或达到更加的效果,接下来就通过一个系列来介绍自定义控件,这里更多是通过一些案例逐步去学习,本系列有一些典型的应用,掌握好了大家也可去创新开发出一些更好的UI,本次先通过简单案例掌握一些基础知识——如何在自定义控件中定义属性.

二、实现定制一个简单RadioButton

  1、编写类型MRadioButton 扩展RadioButton

  1. public class MRadioButton extends RadioButton {
  2. …   
  3. }
复制代码


  2、在MRadioButton类中,定制属性

  我们可以在控件中定义自己的属性,可以定义多个属性,但必须封装提供set/get方法,也就是按规范写。如mValue属性,像下面代码

  1. private String mValue;
  2.     public String getmValue() {
  3.         return mValue;
  4.     }
  5.     public void setmValue(String mValue) {
  6.         this.mValue = mValue;
  7.     }
复制代码


  3、为定制的属性编写attrs.xml资源

  该资源文件放在res/values目录下,内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <declare-styleable name="MRadioButton">
  4.             <! – 属性名称-->
  5.         <attr name="value" format="string" />
  6.     </declare-styleable>
  7. </resources>
复制代码


  4、在MRadioButton类中定义构造函数,初始化属性

  1. public MRadioButton(Context context) {
  2.         super(context);
  3.     }
  4. public MRadioButton(Context context, AttributeSet attrs, int defStyle) {
  5.         super(context, attrs, defStyle);
  6.     }
  7.         public MRadioButton(Context context, AttributeSet attrs) {
  8.         super(context, attrs);
  9.         //从attrs.xml中加载一个名字叫’ .MRadioButton’的declare-styleable资源
  10.     TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.MRadioButton);
  11.         //将属性value与类中的属性mValue关联
  12.     this.mValue = tArray.getString(R.styleable.MRadioButton_value);
  13.         //回收tArray对象
  14.     tArray.recycle();
  15.     }
复制代码


  5、在MainActivity中布局文件中添加MRadioButton组件,如下所示

  1. <RelativeLayout xmlns:android="http#//schemas#android#com/apk/res/android"
  2.     xmlns:tools="http#//schemas#android#com/tools"
  3.     xmlns:jereh="http#//schemas#android#com/apk/res/com#jereh# view"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     tools:context="com#example#zdyview#MainActivity" >
  7. 由于论坛禁止URL链接,此处所有的#,原来应为.
  8.     <com.itc.zidingyiview.MRadioButton
  9.         android:layout_width="match_parent"
  10.         android:layout_height="match_parent"
  11.         android:id="@+id/mrb"
  12.         jereh:value="hello"
  13.         />

  14. </RelativeLayout>
复制代码


  6、MainActivity代码:

  1. public class MainActivity extends Activity {
  2.     private MRadioButton rb;
  3.     @Override
  4.     protected void onCreate(Bundle savedInstanceState) {
  5.         super.onCreate(savedInstanceState);
  6.         setContentView(R.layout.activity_main);
  7.         rb=(MRadioButton)super.findViewById(R.id.mrb);
  8.         rb.setOnClickListener(new View.OnClickListener() {
  9.             @Override
  10.             public void onClick(View v) {
  11. Toast.makeText(MainActivity.this, rb.getmValue(),Toast.LENGTH_LONG).show();
  12.             }
  13.         });
  14.     }
  15. }
复制代码

  当点击单选按钮会显示hello信息


烟台杰瑞教育版权所有!转载请注明出处!



作者: renxiao2003    时间: 2015-04-13 17:12
前几天刚自己做了一个控件,发现自定义控件还是比较方便。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2