主题 : Android自定义控件系列之基础篇(烟台杰瑞教育原创) 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 111595
精华: 0
发帖: 19
金钱: 95 两
威望: 19 点
贡献值: 0 点
综合积分: 38 分
注册时间: 2014-12-31
最后登录: 2016-04-29
楼主  发表于: 2015-03-25 14:51

 Android自定义控件系列之基础篇(烟台杰瑞教育原创)

[table=1126px][tr][td]一、概述[/td][/tr][/table]  在android开发中很多UI控件往往需要进行定制以满足应用的需要或达到更加的效果,接下来就通过一个系列来介绍自定义控件,这里更多是通过一些案例逐步去学习,本系列有一些典型的应用,掌握好了大家也可去创新开发出一些更好的UI,本次先通过简单案例掌握一些基础知识——如何在自定义控件中定义属性.
[table=1126px][tr][td]二、实现定制一个简单RadioButton[/td][/tr][/table]  1、编写类型MRadioButton 扩展RadioButton
    public class MRadioButton extends RadioButton {
    …    
    }
复制代码


  2、在MRadioButton类中,定制属性
  我们可以在控件中定义自己的属性,可以定义多个属性,但必须封装提供set/get方法,也就是按规范写。如mValue属性,像下面代码
    private String mValue;
        public String getmValue() {
            return mValue;
        }
        public void setmValue(String mValue) {
            this.mValue = mValue;
        }
复制代码


  3、为定制的属性编写attrs.xml资源
  该资源文件放在res/values目录下,内容如下:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="MRadioButton">
                <! – 属性名称-->
            <attr name="value" format="string" />
        </declare-styleable>
    </resources>
复制代码


  4、在MRadioButton类中定义构造函数,初始化属性
    public MRadioButton(Context context) {
            super(context);
        }
    public MRadioButton(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
            public MRadioButton(Context context, AttributeSet attrs) {
            super(context, attrs);
            //从attrs.xml中加载一个名字叫’ .MRadioButton’的declare-styleable资源
        TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.MRadioButton);
            //将属性value与类中的属性mValue关联
        this.mValue = tArray.getString(R.styleable.MRadioButton_value);
            //回收tArray对象
        tArray.recycle();
        }
复制代码


  5、在MainActivity中布局文件中添加MRadioButton组件,如下所示
    <RelativeLayout xmlns:android="http#//schemas#android#com/apk/res/android"
        xmlns:tools="http#//schemas#android#com/tools"
        xmlns:jereh="http#//schemas#android#com/apk/res/com#jereh# view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com#example#zdyview#MainActivity" >
    由于论坛禁止URL链接,此处所有的#,原来应为.
        <com.itc.zidingyiview.MRadioButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/mrb"
            jereh:value="hello"
            />

    </RelativeLayout>
复制代码


  6、MainActivity代码:
    public class MainActivity extends Activity {
        private MRadioButton rb;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            rb=(MRadioButton)super.findViewById(R.id.mrb);
            rb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    Toast.makeText(MainActivity.this, rb.getmValue(),Toast.LENGTH_LONG).show();
                }
            });
        }
    }
复制代码

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

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