免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3007 | 回复: 1
打印 上一主题 下一主题

[Android] Android自定义控件系列之基础篇(烟台杰瑞教育原创) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-03-25 14:31 |只看该作者 |倒序浏览
本帖最后由 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信息


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


论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
2 [报告]
发表于 2015-04-13 17:12 |只看该作者
前几天刚自己做了一个控件,发现自定义控件还是比较方便。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP