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,455評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,250評論 4 61
  • 陰天,就是沒有陽光,只有云層。灰蒙蒙的云層層疊疊,厚實地堆積在一起,好像要掉下來卻沒有掉 下來,欲落未落的這種現象...
    棉花兔閱讀 551評論 0 2
  • 每篇課文大概有12個要求認、7個要求寫的生字,每次講課,都在想:為什么就只有12個生字,講了一節課,學生還是記不住...
    薛薛閑扯閱讀 388評論 0 0