Hyena-带眼睛的输入框

Hyena.jpg
引文

AndroidUtilCode这样优秀的库替我管理了工具类,大大提高了我工作的效率,现在工具类终于解放了双手,这得益于AndroidUtilCode,每当构建新项目,只需要一行代码就可以把需要的工具类引用进来,而且这个库还保持着活跃的更新,更多的人在参与进去,开源的力量让AndroidUtilCode变得愈加强大,也使更多的android开发者收益,再次感谢。

Hyena鬣狗快速开发库

也是借鉴了前辈的思路,想成为一个简单好用,保持活力,受大家喜欢的开源库。
核心为快速开发,定位小巧精悍,内容简单精致,整合常用的自定义ViewBase类通用词典

项目中经常需要一个可以显示密码的输入框可以切换明文/密文显示效果,EyesEditText 封装了这个功能,并且将代码全部装进了EyesEditText 中,只需要在xml中使用的时候,根据需要设置效果,使用起来完全没有负担,自带的icon简单大气,还可以设置着色,可以设置失去焦点隐藏icon,可以设置自定义的icon。
使用鬣狗可以快速方便的实现这个功能,详细的使用示例

EyesEditText.png

源码 EyesEditText .java

public class EyesEditText extends AppCompatEditText {

    /**
     * 显示图片
     */
    private Drawable mDrawableVisibility;
    /**
     * 显示关闭图片
     */
    private Drawable mDrawableVisibilityOff;
    /**
     * 跟随焦点
     */
    private boolean mFollowFocus;

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

    public EyesEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public EyesEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.EyesEditTextStyle);
        if (typedArray == null) {
            return;
        }
        //取图片
        int drawableVisibility = typedArray.getResourceId(R.styleable.EyesEditTextStyle_drawable_visibility, R.drawable.design_ic_visibility);
        int drawableVisibilityOff = typedArray.getResourceId(R.styleable.EyesEditTextStyle_drawable_visibility_off, R.drawable.design_ic_visibility_off);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mDrawableVisibility = getContext().getDrawable(drawableVisibility);
            mDrawableVisibilityOff = getContext().getDrawable(drawableVisibilityOff);
        } else {
            mDrawableVisibility = getResources().getDrawable(drawableVisibility);
            mDrawableVisibilityOff = getResources().getDrawable(drawableVisibilityOff);
        }
        if (mDrawableVisibility == null) {
            return;
        }
        mDrawableVisibility.mutate();
        if (mDrawableVisibilityOff == null) {
            return;
        }
        mDrawableVisibilityOff.mutate();

        //取着色
        int color = typedArray.getColor(R.styleable.EyesEditTextStyle_drawable_tint, -1);
        ColorStateList colorStateList = color == -1 ? getTextColors(): ColorStateList.valueOf(color);
        DrawableCompat.setTintList(mDrawableVisibility, colorStateList);
        DrawableCompat.setTintList(mDrawableVisibilityOff, colorStateList);

        //取是否跟随焦点
        mFollowFocus = typedArray.getBoolean(R.styleable.EyesEditTextStyle_drawable_follow_focus, false);

        typedArray.recycle();

        checkDrawableVisible();
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        checkDrawableVisible();
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        checkDrawableVisible();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                Drawable drawable = getCompoundDrawables()[2];
                if (drawable != null && event.getX() <= (getWidth() - getPaddingRight()) && event.getX() >= (getWidth() - getPaddingRight() - drawable.getBounds().width())) {
                    if (getTransformationMethod() instanceof PasswordTransformationMethod) {
                        setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    } else {
                        setTransformationMethod(PasswordTransformationMethod.getInstance());
                    }
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    /**
     * 判断是否显示图片
     */
    private void checkDrawableVisible() {
        if (mFollowFocus) {
            setDrawableVisible(hasFocus() && length() > 0);
        } else {
            setDrawableVisible(length() > 0);
        }
    }

    /**
     * 设置图片显示状态
     */
    private void setDrawableVisible(boolean visible) {
        Drawable drawable;
        if (getTransformationMethod() instanceof PasswordTransformationMethod) {
            drawable = mDrawableVisibility;
        } else {
            drawable = mDrawableVisibilityOff;
        }
        setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], getCompoundDrawables()[1]
                , visible ? drawable : null, getCompoundDrawables()[3]);
    }
}

更多功能请前往Github查看,传送门: Hyena鬣狗快速开发库

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,970评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,267评论 4 61
  • 阴天,就是没有阳光,只有云层。灰蒙蒙的云层层叠叠,厚实地堆积在一起,好像要掉下来却没有掉 下来,欲落未落的这种现象...
    棉花兔阅读 561评论 0 2
  • 每篇课文大概有12个要求认、7个要求写的生字,每次讲课,都在想:为什么就只有12个生字,讲了一节课,学生还是记不住...
    薛薛闲扯阅读 398评论 0 0