自定义控件SettingItemView
一、效果图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:zengmg="http://schemas.android.com/apk/res/com.zengmg.MobileSafe"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewstyle="@style/ActivityTitle"android:text="@string/settingTitle" /><com.zengmg.MobileSafe.view.SettingItemViewandroid:id="@+id/siv_update"android:layout_width="match_parent"android:layout_height="wrap_content"zengmg:desc_off="自动更新关闭"zengmg:desc_on="自动更新开启"zengmg:title="自动更新设置" /></LinearLayout>
二、步骤
三、各步骤代码
3.1布局文件:activity_setting.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:zengmg="http://schemas.android.com/apk/res/com.zengmg.MobileSafe"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewstyle="@style/ActivityTitle"android:text="@string/settingTitle" /><com.zengmg.MobileSafe.view.SettingItemViewandroid:id="@+id/siv_update"android:layout_width="match_parent"android:layout_height="wrap_content"zengmg:desc_off="自动更新关闭"zengmg:desc_on="自动更新开启"zengmg:title="自动更新设置" /></LinearLayout>
xmlns:zengmg="http://schemas.android.com/apk/res/com.zengmg.MobileSafe"
zengmg:desc_off="自动更新关闭"
zengmg:desc_on="自动更新开启"
zengmg:title="自动更新设置"
3.2 自定义 attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="SettingItemView"><attr name="title" format="string" /><attr name="desc_on" format="string" /><attr name="desc_off" format="string" /></declare-styleable><!-- 在D:\ADT\sdk\platforms\android-16\data\res\values\attrs.xml 文件中找 name="TextView"。name="SettingItemView" 是控件名--></resources>
3.3 自定义控件代码 SettingItemView.java
package com.zengmg.MobileSafe.view;import com.zengmg.MobileSafe.R;import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;/*** 设置页面的自定义控件* 布局文件在:view_setting_item.xml* 属性文件在:values/attrs.xml。(通过查看Android自带的控件找到的)* * @author zengmg* */
public class SettingItemView extends RelativeLayout {private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.zengmg.MobileSafe";private TextView tvTitle;private TextView tvDesc;private CheckBox cbState;private String mtitle;private String mDescOn;private String mDescOff;public SettingItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public SettingItemView(Context context, AttributeSet attrs) {super(context, attrs);mtitle=attrs.getAttributeValue(NAMESPACE, "title");mDescOn=attrs.getAttributeValue(NAMESPACE, "desc_on");mDescOff=attrs.getAttributeValue(NAMESPACE, "desc_off");initView();}public SettingItemView(Context context) {super(context);}public void initView(){//View android.view.View.inflate(Context context, int resource, ViewGroup root)//将自定义好的布局文件设置给当前的SettingItemView控件//当SettingItemView控件被使用时,文件view_setting_item里的布局就被显示了View.inflate(getContext(), R.layout.view_setting_item, this);tvTitle=(TextView) findViewById(R.id.tv_title);tvDesc=(TextView) findViewById(R.id.tv_desc);cbState=(CheckBox) findViewById(R.id.cb_status);setMtitle(mtitle);}public String getMtitle() {return tvTitle.getText().toString();}public void setMtitle(String mtitle) {tvTitle.setText(mtitle);}public String getmDescOn() {return tvDesc.getText().toString();}public void setmDescOn(String mDescOn) {tvDesc.setText(mDescOn);}public String getmDescOff() {return tvDesc.getText().toString();}public void setmDescOff(String mDescOff) {tvDesc.setText(mDescOff);}public boolean isChecked() {return cbState.isChecked();}public void setChecked(boolean mIsChecked) {cbState.setChecked(mIsChecked);if(mIsChecked){tvDesc.setText(mDescOn);}else{tvDesc.setText(mDescOff);}}}
有关
View.inflate(getContext(), R.layout.view_setting_item, this);
3.4 在activity使用的代码
package com.zengmg.MobileSafe.activity;import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;import com.zengmg.MobileSafe.R;
import com.zengmg.MobileSafe.view.SettingItemView;public class SettingActivity extends Activity{private SettingItemView sivUpdate;private SharedPreferences mPref;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_setting);mPref = getSharedPreferences("config", MODE_PRIVATE);sivUpdate=(SettingItemView) findViewById(R.id.siv_update);boolean autoUpdate=mPref.getBoolean("auto_update", true);sivUpdate.setChecked(autoUpdate);sivUpdate.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if(sivUpdate.isChecked()){//不自动更新sivUpdate.setChecked(false);mPref.edit().putBoolean("auto_update", false).commit();}else{//自动更新sivUpdate.setChecked(true);mPref.edit().putBoolean("auto_update", true).commit();}}});}
}
自定义控件SettingItemView相关推荐
- android中设置控件的搞,Android中如何自定义控件
Android开发中难免遇到需要自定义控件的需求,有些是产品的要求在Android标准控件库中没有满足要求的,有些是开发过程中没有代码的可复用,自己定义的. 一个好的自定义控件应当和Android本身 ...
- 如何给自定义控件添加自定义属性
1.在values目录下创建一个attrs.xml文件 <?xml version="1.0" encoding="utf-8"?> <res ...
- 开发Eclipse自定义控件
摘自:http://www.ibm.com/developerworks/cn/opensource/os-eclipcntl/ 我们在开发自定义控件时主要考虑以下问题: 1. 自定义控件的绘制:通常 ...
- qt获取当前系统音量值_Qt编写自定义控件50-迷你仪表盘
一.前言 这个控件取名叫迷你仪表盘,是以为该控件可以缩小到很小很小的区域显示,非常适合小面积区域展示仪表数据使用,还可以手动触摸调节进度,是我个人觉得最漂亮小巧的一个控件.初次看到类似的控件是在一个音 ...
- 【iOS】自定义控件入门:可拖动的环形进度
有时候UIKit的标准控件并不能满足我们的需求,因此我们可以通过自定义控件得到满足我们需求的控件,例如这篇文章将教你如何自定义一个圆形的进度条,并且用户可以通过拖动进度条上的手柄来改变进度值.主要参考 ...
- Android自定义控件系列之基础篇
一.概述 在android开发中很多UI控件往往需要进行定制以满足应用的需要或达到更加的效果,接下来就通过一个系列来介绍自定义控件,这里更多是通过一些案例逐步去学习,本系列有一些典型的应用,掌握好了大 ...
- C#自定义控件四简易时钟
C#自定义控件四简易时钟 效果图: 简易时钟,顾名思义,简单容易,简单到什么程度呢?界面只有数字和指针,甚至连与当前时间都不能匹配!呵呵!就这么简单,学习嘛,从简单开始. 毫无疑问,这里肯定要用到Ti ...
- 对做C#自定义控件的一点心得
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装这个COM组件,中间遇到很多曲折,研究了一个星期,终于完成了 下面总结一下我做DSOFramer这个自定义控件的注意地方: 1.在创建 ...
- ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl
第四章 组合控件开发CompositeControl 大家好,今天我们来实现一个自定义的控件,之前我们已经知道了,要开发自定义的控件一般继承三个基 类:Control,WebControl,还有一个就 ...
最新文章
- swift语言的Block
- C++实现各种选择排序(简单选择排序,堆排序)
- About static contructor API changes in cocos2d-...
- sql不能使用OpenRowset
- TcpTrace实现的基本原理
- Critical dependency: require function dependencies cannot be statically extracted
- 通过本地上传工具把CSV文件导入到百会报表
- 如何用excel快速实现“平均值±标准差”
- 萨达阿萨德发送到在线橙V
- 项目如行军——《孙子兵法》之九地篇
- 计算机音乐代表人物,在你的周围,一定有许多名人吧!比如:故事大王xxx,电脑高手xxx,音乐家xxx……请你选择其中一...
- Ubuntu 下PupBot 搭建QQ机器人
- Java算法七:骑士走棋盘
- j3455跑mysql_基于J3455搭建NAS
- CSS齿轮转动加载动画
- 很好的例子理解区别 Maximum Likelihood (ML) Maximum a posteriori (MAP)
- 浏览器审查元素查找网页元素对应代码
- 【O2O领域】Axure外卖配送代理商调度系统原型,生鲜配送站团队管理后台原型
- ps2022安装包程序文件错误,ps2022安装包在哪儿下载
- ubuntu安装mysql忘记密码并重置
热门文章
- c语言case key pres,C#程序设计B-中国大学mooc-题库零氪
- java 日期操作工具类_java8操作日期的工具类
- 快捷键_AutoCAD 2021中的默认快捷键、新建或编辑快捷键
- streaming接mysql数据库_[Spark streaming举例]-- 实时统计并且存储到mysql数据库中
- 五、Python第五课——Python中组织列表的相关函数
- MBR、DBR、FAT32基础小知识
- 清理offset_关于 kafka 日志清理策略的问题
- mysql 高级知识点_这是我见过最全的《MySQL笔记》,涵盖MySQL所有高级知识点!...
- Linux内存背后的那些神秘往事
- mysql 5.5 client 字符集_rhel4 mysql5.5 字符集_character set