博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android之自定义组合控件
阅读量:4289 次
发布时间:2019-05-27

本文共 2069 字,大约阅读时间需要 6 分钟。

---------------setting_item---------------

//下面的focusable和clickable是chekbox天生就有的属性,点击的优先级比组合控件高,所以吧组合控件设置了点击事件,就药吧checkbox的点击事件去掉
/** * Created by lambo on 2018/4/15. * 自定义组合控件---多个控件组成,可以复用 */public class Setting_item extends RelativeLayout {CheckBox cb;    private TextView tv_subtile;    private TextView tv_title;    private String descoff;    private String descon;    private String title;    //带一个参数的构造方法,初始化布局文件的时候调用    public Setting_item(Context context) {        super(context);        initView(context);    }    private void initView(Context context) {        //吧布局文件转换成view        View.inflate(context, R.layout.setting_item, this);        cb=findViewById(R.id.ck_setting_item);        tv_subtile = findViewById(R.id.tv_setting_item_subtitle);        tv_title = findViewById(R.id.tv_settting_item_title);    }/*带两个参数的构造方法,布局文件使用的时候调用*/    public Setting_item(Context context, AttributeSet attrs) {        super(context, attrs);        initView(context);        //获取命名空间(http://schemas.android.com/apk/res-auto)下的属性title,descon,descoff,在attr.xml中给textview中扩展了属性        title = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","title");        descon = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","descon");        descoff = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","descoff");        tv_title.setText(title);    }    public Setting_item(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initView(context);    }//检验组合控件是否选中    public Boolean isChecked(){        return cb.isChecked();    }    //设置组合控件    public void setChecked(Boolean isChecked){        if(isChecked){            setSubtile(descon);        }else {            setSubtile(descoff);        }        cb.setChecked(isChecked);    }    //设置组合控件自标题的方法    public void setSubtile(String  subtilte){tv_subtile.setText(subtilte);    }}

---------------------res/values/attr.xml-----------------添加属性

//给自定义的组合控件Setting_item扩展属性

转载地址:http://ttmgi.baihongyu.com/

你可能感兴趣的文章
JavaScript事件详解-zepto的事件实现(二)
查看>>
MySql表信息基础知识学习
查看>>
为什么document找到的不是html节点_优就业
查看>>
Javascript本地存储小结
查看>>
常用排序方法介绍
查看>>
Java异常分类和统一处理
查看>>
原 荐 cache线程池对数据库操作的饥饿问题
查看>>
使用Eclipse把java文件打包成jar 含有第三方jar库的jar包
查看>>
3种web会话管理的方式
查看>>
SSM(框架)-异常1:面向接口式编程异常
查看>>
Android蓝牙4.0之玩爆智能穿戴、家具(二)
查看>>
使用Condition实现多线程之间调用
查看>>
javaAPI之String
查看>>
JQ 新窗口打开链接并设置参数
查看>>
JQuery实现列表中复选框全选反选功能封装
查看>>
JAVA GC 简单总结
查看>>
JS中常遇到的浏览器兼容问题和解决方法
查看>>
JAVA学习笔记之-servlet知识点
查看>>
apache 配置不同的端口访问不同的站点
查看>>
2017年3月Java9带来的革新!
查看>>