EditText自带显示隐藏密码

package com.jiutouxiang.activity.login;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.jiutouxiang.R;

/**
 * Created by Administrator on 2016/9/29.
 */

public class PasswordEditText extends EditText implements View.OnFocusChangeListener,TextWatcher {
//EditText右侧眼睛图标
private Drawable mLookDrawable;
private Drawable mClearDrawable;
private boolean hasFoucs;
//判断是否可见
private boolean visible=false;
public PasswordEditText(Context context){
super(context,null);
}
public PasswordEditText(Context context, AttributeSet attris){
this(context,attris, android.support.v7.appcompat.R.attr.editTextStyle);
init();
}
public PasswordEditText(Context context, AttributeSet attrist, int defStyle){
super(context,attrist,defStyle);
init();
}
private void init(){
// 获取EditText的DrawableRight,假如没有设置我们就使用默认的图片,获取图片的顺序是左上右下(0,1,2,3,)
mLookDrawable=getCompoundDrawables()[2];
if(mLookDrawable==null){
mLookDrawable=getResources().getDrawable(R.mipmap.login_password_normal);
mClearDrawable=getResources().getDrawable(R.mipmap.login_edittext_clear);
}
mLookDrawable.setBounds(0,0,mLookDrawable.getIntrinsicWidth(),mLookDrawable.getIntrinsicHeight());

//默认设置图标
setClearIconVisible(true);
//设置焦点改变的监听
setOnFocusChangeListener(this);
//设置输入框内容发生改变的监听
addTextChangedListener(this);
}
 /* @说明:isInnerWidth, isInnerHeight为ture,触摸点在删除图标之内,则视为点击了删除图标
 * event.getX() 获取相对应自身左上角的X坐标
 * event.getY() 获取相对应自身左上角的Y坐标
 * getWidth() 获取控件的宽度
 * getHeight() 获取控件的高度
 * getTotalPaddingRight() 获取删除图标左边缘到控件右边缘的距离
 * getPaddingRight() 获取删除图标右边缘到控件右边缘的距离
 * isInnerWidth:
 *  getWidth() - getTotalPaddingRight() 计算删除图标左边缘到控件左边缘的距离
 *  getWidth() - getPaddingRight() 计算删除图标右边缘到控件左边缘的距离
 * isInnerHeight:
 *  distance 删除图标顶部边缘到控件顶部边缘的距离
 *  distance + height 删除图标底部边缘到控件顶部边缘的距离
 */
@Override
public boolean onTouchEvent(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_UP) {
if (getCompoundDrawables()[2] != null) {
int x = (int)event.getX();
int y = (int)event.getY();
Rect rect = getCompoundDrawables()[2].getBounds();
int height = rect.height();
int distance = (getHeight() - height)/2;
boolean isInnerWidth = x > (getWidth() - getTotalPaddingRight()) && x < (getWidth() - getPaddingRight());
boolean isInnerHeight = y > distance && y <(distance + height);
if (isInnerWidth && isInnerHeight) {
//如果点击
//this.setText("");
if(visible){
//需要隐藏
this.setTransformationMethod(PasswordTransformationMethod.getInstance());
visible=false;
// 获取EditText的DrawableRight,假如没有设置我们就使用默认的图片,获取图片的顺序是左上右下(0,1,2,3,)

mLookDrawable=getResources().getDrawable(R.mipmap.login_password_normal);

mLookDrawable.setBounds(0,0,mLookDrawable.getIntrinsicWidth(),mLookDrawable.getIntrinsicHeight());
//默认设置图标
setClearIconVisible(true);
Log.e("需要隐藏","需要隐藏");
}else{
//需要看见

   this.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
visible=true;
// 获取EditText的DrawableRight,假如没有设置我们就使用默认的图片,获取图片的顺序是左上右下(0,1,2,3,)

mLookDrawable=getResources().getDrawable(R.mipmap.login_password_selected);
//getIntrinsicWidth()和getIntrinsicHeight()返回的宽高应该是dp为单位的哦。
mLookDrawable.setBounds(0,0,mLookDrawable.getIntrinsicWidth(),mLookDrawable.getIntrinsicHeight());
//默认设置图标
setClearIconVisible(true);
Log.e("需要看见","需要看见");
}
}
}
}
return super.onTouchEvent(event);
}

/**
 * 当ClearEditText焦点发生变化的时候,
 * 输入长度为零,隐藏删除图标,否则,显示删除图标
 */
@Override
public void onFocusChange(View v, boolean hasFocus) {
//获取焦点的情况
/*this.hasFoucs= hasFocus;
if (hasFocus) {
setClearIconVisible(getText().length() > 0);
} else {
setClearIconVisible(false);
}*/
}


@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
//内部数据发生改变时
/*if (hasFoucs) {
setClearIconVisible(s.length() > 0);
}*/
}

private void setClearIconVisible(boolean visibke){
Drawable right=visibke?mLookDrawable:null;
setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1],right,getCompoundDrawables()[3]);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void afterTextChanged(Editable s) {

}
}

xml文件直接引用就好了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,967评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,420评论 25 708
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,643评论 0 4
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • 第一次见你实在那次你摆好造型的出场吧 总觉得这个男的也太高调了吧 但是一次又一次的接触 一次又一次的认真了解 才明...
    名字666阅读 221评论 0 0