[Android学习笔记四] 自定义Android组件之组合方式创建密码框组件
Android中所有控件(也称组件)都继承自adnroid.view.View类,android.view.ViewGroup是View类的重要子类,绝大多书的布局类就继承自ViewGroup类。
参见:基于Android Api21的View和Widget类图
自定义Android组件基本可以从2个入口着手,一是继承Viewe类拿起画笔和画布绘制组件,而是通过继承View的子类和组合已有的组件的方式构造自定义的组件。
本文通过自定义一个PassWordView组件来实现密码能够通过点击点选框来决定是否显示。PassWordView组件类通过继承LinearLayout类和组合EditText,CheckBox组件实现。
效果如下图:
一: 实现上述密码框组件需要以下步骤:
a. 设计PassWordView组件
b. 自定义样式属性,res/value/attrs.xml
c. 创建PassWordView
c. 实现PassWordView的功能,如:事件,属性设置,组合组件
二: 使用密码框组件:
a. 使用PassWordView组件,设置属性
b. 在Activity中对其进行操作
三: 案例:
1. 密码框组件主要是组合EditText和CheckBox组件,通过CheckBox的选中状态来决定密码是否显示为明文,EditText和CheckBox组件要求并列一行。故采用LinearLayout布局。
2. 密码框组件的自定义样式属性,通常根据具体需要来定义。
PassWordView自定义组件中称EditText为left Component(左组件),CheckBox为right Component(右组件)。
定义样式属性(位置文件:res/value/attrs.xml):
<declare-styleable name="PassWordView"><!--直接使用系统中已定义的语意明确的属性,不用设置format--><attr name="android:inputType"/><!--自定义属性--><attr name="left_component_weight" format="float"/><attr name="right_component_weight" format="float"/></declare-styleable>
其中inputType使用了android系统已经定义的属性,注意书写格式。
3. 实现PassWordView类
package secondriver.viewlibrary;import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;/*** PassWordView* <p/>* leftComponent是一个EditText* RightComponent是一个CheckBox* <p/>* Author : secondriver* Created : 2015/11/25*/
public class PassWordView extends LinearLayout {private EditText mPassWordEditText;private CheckBox mShowCheckBox;private LinearLayout.LayoutParams mPassWordParams;private LinearLayout.LayoutParams mShowParams;private float leftComponentWeight;private float rightComponentWeight;private int inputType;public PassWordView(Context context, AttributeSet attrs) {super(context, attrs);initProperty(context, attrs);initComponent(context);}private final void initProperty(Context context, AttributeSet attrs) {//获取设置的值,未设置使用默认值TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PassWordView);leftComponentWeight = typedArray.getFloat(R.styleable.PassWordView_left_component_weight, 5.0f);rightComponentWeight = typedArray.getFloat(R.styleable.PassWordView_right_component_weight, 1.0f);//来自Android系统已定义的属性inputType = typedArray.getInt(R.styleable.PassWordView_android_inputType, InputType.TYPE_TEXT_VARIATION_PASSWORD);typedArray.recycle();}private void initComponent(Context context) {mPassWordEditText = new EditText(context);mPassWordEditText.setInputType(inputType);mPassWordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());mShowCheckBox = new CheckBox(context);//通过权重来决定EditText和CheckBox占据父视图的空间的比例mPassWordParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, leftComponentWeight);mShowParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, rightComponentWeight);//父视图是一个容器视图,指定水平排列子视图setOrientation(HORIZONTAL);addView(mPassWordEditText, mPassWordParams);addView(mShowCheckBox, mShowParams);addCheckBoxListener();}/*** 获取PassWord** @return*/public String getPassWord() {return mPassWordEditText.getText().toString();}/*** 获取PassWord组件** @return*/public EditText getPassWordEditText() {return mPassWordEditText;}private final void addCheckBoxListener() {/*** CheckBox点击事件处理** 如果选中EditText中的密码明文显示** 如果未选中EditText中的密码黑点显示**/mShowCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {mPassWordEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());} else {mPassWordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());}}});}
}
4. 使用PassWordView组件
文件:activity_combine_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:passwordview="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><secondriver.viewlibrary.PassWordViewandroid:id="@+id/password_view"android:layout_width="300dp"android:layout_height="wrap_content"android:layout_margin="10dp"android:background="#565354"android:inputType="textPassword"android:padding="10dp"passwordview:left_component_weight="5.0"passwordview:right_component_weight="1.0"/><Button android:layout_width="match_parent" android:layout_height="wrap_content"android:onClick="onShowPassWord"android:text="获取密码"/><TextViewandroid:id="@+id/password_text_view"android:layout_width="match_parent" android:layout_height="wrap_content"android:hint="显示password"/> </LinearLayout>
文件:CombinePassWordViewActivity.java
package secondriver.sdk.activity;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;import secondriver.sdk.R;
import secondriver.viewlibrary.PassWordView;/*** Author : secondriver* Created : 2015/11/25*/
public class CombinePassWordViewActivity extends Activity {private PassWordView mPassWordView;private TextView mPassWordTextView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_combine_view);mPassWordView = (PassWordView) findViewById(R.id.password_view);mPassWordTextView = (TextView) findViewById(R.id.password_text_view);}public void onShowPassWord(View view) {mPassWordTextView.setText(mPassWordView.getPassWord());}
}
AndroidManifest.xml中添加CombinePassWordViewActivity即可。
5.结果
如文中一开始展示的效果图
6. 总结
本文主要演示的通过组合的方式实现自定义Android组件,一般情况通过组合已有的的组件来实现复杂组件相对更容易一些,也能够得到组件重用的福利;现有组件不能满足的情况下可以考虑绘制组件,该方式最为原始和灵活。
[Android学习笔记四] 自定义Android组件之组合方式创建密码框组件相关推荐
- Android学习笔记07---查看Android虚拟机输出的错误信息与如何部署应用到自己的真实手机
Android学习笔记07---查看Android虚拟机输出的错误信息
- Android学习笔记----解决“com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536”问题
Android学习笔记----解决"com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 6553 ...
- Android学习笔记四十Preference使用
Preference直译为偏好,博友建议翻译为首选项.一些配置数据,一些我们上次点击选择的内容,我们希望在下次应用调起的时候依旧有效,无须用户再一次进行配置或选择.Android提供preferenc ...
- Android学习笔记之自定义Toast
1)布局文件layout [html] view plaincopy <RelativeLayout xmlns:android="http://schemas.android.com ...
- Android 学习笔记四:创建工具栏按钮
原文:http://blog.csdn.net/lihongxun945/article/details/48951199 前面我们已经可以在一个Activity中添加一些按钮之类的组件.由于手机的屏 ...
- Pro Android学习笔记 四八 ActionBar 1 Home图标区
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Acti ...
- 微信小程序学习笔记(四)自定义组件
文章目录 1. 组件的创建与引用 1.1 创建组件 1.2 引用组件 1.3 全局引用 VS 局部引用 1.4 组件和页面的区别 2. 样式 2.1 组件样式隔离 2.2 修改组件的样式隔离选项 3. ...
- Android学习笔记(四):android画图之paint之setXfermode
2019独角兽企业重金招聘Python工程师标准>>> setXfermode 设置两张图片相交时的模式 我们知道 在正常的情况下,在已有的图像上绘图将会在其上面添加一层新的形状. ...
- Android学习笔记:对Android应用进行单元测试
第一步:在AndroidManifest.xml中加入如下两段代码: [java] view plaincopyprint? <manifest xmlns:android="h ...
最新文章
- linux mysql定时备份并压缩
- DispacherServlet默认加载配置文件名
- ubuntu 20.04 DNS 设置
- [Java]jvm参数选项中文文档
- 判断一个数是否为质数
- android开发手机字体大小设置,安卓代码动态设置字体大小
- 映射的网络驱动器怎么共享_如何在Windows上通过网络共享CD和DVD驱动器
- 如何制作计算机启动盘,电脑怎么制作U盘启动盘
- 文件二维码:在线直接扫一扫二维码查看下载资料
- 六度空间-c++实现
- MATLAB超限邻域滤波
- java多数据库开发evn,Java,在多线程evnironments中通过散列统一划分传入的工作
- 计算机网络(三)数据链路层详解
- Mybatis配置注意事项
- 一个简单的python爬虫程序
- Ubuntu服务器上运行mma,一、MMA概述
- 公平性、差异性、均衡性的衡量方法
- 2013年10月17日_羊豆豆_新浪博客
- IdCardGenerator生成工具
- 基于spark的Scala编程—读取properties文件
热门文章
- \pset 、\x命令
- python库tkinter、pygame中几点需要注意的问题
- Python学习总结之四 -- 这就是Python的字典
- poj 2723 2-SAT问题
- div隐藏select显示的解决办法(就死select挡住了div) 不是网上找到的那五种
- gdal数据类型_科学网-gdal数据类型的代码的核心定义文件-林清莹的博文
- java url 短链接_推荐几个官方腾讯短链接url接口(含PHP演示代码)
- 计算机相关报道的观后感,《新闻报道》观后感800字
- hashmultimap java_【Java 学习笔记】 HashMultimap(guava)
- dns bind配置教程