自定义查看密码输入框

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.hanergy.solarfac.R;
/**
 * @author yxf
 * @data 2017/5/28 17:30
 * @description 自定义带查看密码功能输入框
 */
public class PwdEditText extends android.support.v7.widget.AppCompatEditText {
    private Context mContext;
    private Drawable imgInable;
    private Drawable imgAble;
    //是否可以切换
    private boolean isChange;
    //是否显示密码
    private boolean isShow;

    public PwdEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
        // TODO Auto-generated constructor stub
    }

    public PwdEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
        // TODO Auto-generated constructor stub
    }

    public PwdEditText(Context context) {
        super(context);
        mContext = context;
        init();
        // TODO Auto-generated constructor stub
    }

    /**
     * 初始化
     */
    private void init() {
        imgInable = ContextCompat.getDrawable(mContext,R.drawable.btn_pwdedittext_password_hide);
        imgAble = ContextCompat.getDrawable(mContext,R.drawable.btn_pwdedittext_password_show);
        setCompoundDrawablesWithIntrinsicBounds(null, null, imgInable, null);
        addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                setDrawable();
            }
        });
        setDrawable();
    }

    /**
     * 设置图片
     */
    protected void setDrawable() {
        // WithIntrinsicBounds表示方法会给Drawable对象一个默认的尺寸,如无需控制Drawable对象尺寸,不需要对图片做预先处理
        if (length() < 1) {
            isChange = false;
            setCompoundDrawablesWithIntrinsicBounds(null, null, imgInable, null);
        } else
            isChange = true;

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        if (imgInable != null && event.getAction() == MotionEvent.ACTION_UP) {
            int eventX = (int) event.getRawX();//获取触碰点X轴坐标
            int eventY = (int) event.getRawY();//获取触碰点Y轴坐标
            //创建矩形区域对象
            Rect rect = new Rect();
            this.getGlobalVisibleRect(rect);// 获取视图在屏幕坐标中的可视区域(16, 148 - 226, 188)像素px表示左上右下
            rect.left = rect.right - 50;
            if (rect.contains(eventX, eventY)) {
                if (!isChange) {
                    return super.onTouchEvent(event);
                }
                if (!isShow) {
                    //显示密码
                    setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null);
                    setSelection(length());// 设置光标的位置
                    String digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
                    setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    setKeyListener(DigitsKeyListener.getInstance(digits));
                } else {
                    setCompoundDrawablesWithIntrinsicBounds(null, null, imgInable, null);
                    setSelection(length());// 设置光标的位置
                    String digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
                    setTransformationMethod(PasswordTransformationMethod.getInstance());
                    setKeyListener(DigitsKeyListener.getInstance(digits));
                }

                isShow = !isShow;
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void finalize() throws Throwable {
        //这是被系统调用的方法,系统会根据系统环境来调用,对于程序来说它的调用实际不可预见
        super.finalize();
    }

}


版权声明:本文为u014408122原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。