仿微信password控件,实现密码可见于不可见功能
2016-01-25 11:26
411 查看
1、控件源码VisiblePasswordEditText .java
package cn.com.dsyc.canteenappforseller.widget; import cn.com.dsyc.canteenappforseller.R; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.inputmethod.EditorInfo; import android.widget.EditText; public class VisiblePasswordEditText extends EditText { private Drawable mPasswordShow; private Drawable mPasswordHide; private boolean mHide = true; public VisiblePasswordEditText(Context context) { super(context); // TODO Auto-generated constructor stub if (!isInEditMode()) init(); } public VisiblePasswordEditText(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub if (!isInEditMode()) init(); } public VisiblePasswordEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub if (!isInEditMode()) init(); } private void init() { mPasswordShow = getResources().getDrawable(R.drawable.password_show); mPasswordHide = getResources().getDrawable(R.drawable.password_hide); mPasswordShow.setBounds(0, 0, mPasswordShow.getIntrinsicWidth(), mPasswordShow.getIntrinsicHeight()); mPasswordHide.setBounds(0, 0, mPasswordHide.getIntrinsicWidth(), mPasswordHide.getIntrinsicHeight()); setHeight(mPasswordHide.getIntrinsicHeight() + getResources().getDimensionPixelSize(R.dimen.one_dp_padding) * 5); update(); setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if (getCompoundDrawables()[2] != null) { if (event.getAction() == MotionEvent.ACTION_UP && event.getX() > getWidth() - getPaddingRight() - mPasswordHide.getIntrinsicWidth()) { mHide = !mHide; update(); } } return false; } }); } private void update() { Drawable[] drawables = getCompoundDrawables(); if (mHide) { setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD); setCompoundDrawables(drawables[0], drawables[1], mPasswordHide, drawables[3]); } else { setInputType(EditorInfo.TYPE_CLASS_TEXT); setCompoundDrawables(drawables[0], drawables[1], mPasswordShow, drawables[3]); } setSelection(getText().length()); } }
相关文章推荐
- 客户端密码控件(password)刷新页面保存值的解决方案
- 微信打开第三方应用
- 微信开发之 获取用户地址
- 微信开发 之 自定义菜单
- 微信企业号如何在十分钟内发送一万个红包
- 【微信你妹】中间人攻击截获微信数据
- wemall——让你意想不到的微信商城的机遇
- 模仿微信的多图片上传功能
- 一百多篇热门经典计算文章 来自 11 个热门的技术类微信公众
- 基于百度BAE快速部署微信公众号后台服务程序
- 支撑10亿个微信红包收发的技术秘密(组图)
- 微信蓝牙Airsync协议demo工程实现
- 用zepto显示类似微信上下翻页的实现代码
- android 微信支付 问题解决
- 微信企业号开发(3)--建立连接(回调)
- Android仿微信主界面设计
- 春节红包:微信、QQ与支付宝的社交、支付攻防战
- 解决微信下拉
- JSAPI微信支付开发流程和坑
- 一个Java写的批量重命名文件小程序