Android-自定义带有密码隐藏显示功能的EditText

   日期:2020-10-29     浏览:96    评论:0    
核心提示:文章目录自定义的EditText类布局效果自定义的EditText类思路和Android-自定义带有删除图标的EditText差不多package com.android02;import android.annotation.SuppressLint;import android.content.Context;import android.graphics.drawable.Drawable;import android.os.Build;import android.suppor

文章目录

  • 自定义的EditText类
  • 布局
  • 效果

自定义的EditText类

思路和Android-自定义带有删除图标的EditText差不多

package com.android02;


import android.annotation.SuppressLint;
import android.content.Context;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;

import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Toast;



public class VisiblePwd extends AppCompatEditText implements View.OnFocusChangeListener, TextWatcher { 

    
    private Drawable clearDrawable;

    // 控件是否有焦点
    private boolean hasFocus;

    //明文还是密文的标志,默认密文
    public boolean hasHide=true;


    public VisiblePwd(Context context) { 
        this(context, null);
    }

    public VisiblePwd(Context context, AttributeSet attrs) { 
        // 这里构造方法也很重要,不加这个很多属性不能再XML里面定义
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public VisiblePwd(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle);
        init();
    }

    private void init() { 
        // 获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
        clearDrawable = getCompoundDrawables()[2];
        if (clearDrawable == null) { 
            // throw new
            // NullPointerException("You can add drawableRight attribute in XML");
            clearDrawable = ContextCompat.getDrawable(getContext(),R.drawable.hide);
        }

        // 设置图标的大小
        clearDrawable.setBounds(0, 0, 80, 80);

        // 默认设置隐藏图标
        setClearIconVisible(false);
        // 设置焦点改变的监听
        setOnFocusChangeListener(this);
        // 设置输入框里面内容发生改变的监听
        addTextChangedListener(this);
    }

    

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public boolean onTouchEvent(MotionEvent event) { 
        if (event.getAction() == MotionEvent.ACTION_UP) { 
            if (getCompoundDrawables()[2] != null) { 

                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight()) && (event.getX() < ((getWidth() - getPaddingRight())));

                if (touchable&&getText().length()>0) { 
                    if(hasHide){ 
                        clearDrawable = ContextCompat.getDrawable(getContext(),R.drawable.eye);
                        //设置图标
                        setCompoundDrawablesWithIntrinsicBounds(null,null,clearDrawable,null);

                        //将input的类型修改成text,明文
                        setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
                        setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                        hasHide = false;
                    }
                    else{ 

                       
                        clearDrawable = ContextCompat.getDrawable(getContext(),R.drawable.hide);
                        setCompoundDrawablesWithIntrinsicBounds(null,null,clearDrawable,null);
                        //将input的类型修改成password,密文
                        setTransformationMethod(PasswordTransformationMethod.getInstance());
                        setInputType(EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                        hasHide = true;

                    }

                }
            }
        }
        return super.onTouchEvent(event);
    }



    
    @Override
    public void onFocusChange(View v, boolean hasFocus) { 
        this.hasFocus = hasFocus;
        if (hasFocus) { 
            setClearIconVisible(getText().length() > 0);
        } else { 
            setClearIconVisible(false);
        }
    }

    
    protected void setClearIconVisible(boolean visible) { 

        Drawable right = visible ? clearDrawable : null;
        //设置图标大小
        clearDrawable.setBounds(0,0,80,80);
        setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);



    }

    
    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after) { 
        if (hasFocus) { 
            setClearIconVisible(s.length() > 0);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    }

    @Override
    public void afterTextChanged(Editable s) { 

    }

}

布局

将EditText改为我们自定义的EditText就行了(包名+类名)

效果


 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服