自定义EditText控制DrawableRight的显示或隐藏

目的:

有时候在做搜索功能时,如果EditText有内容,则在右侧显示一个删除图标,点击删除图标删除内容;

当EditText内容为空时,则隐藏删除图标。

效果图:

p.s. 这是我正在做的一个毕业设计,Android + JavaWeb。前端app,后台都会有。我每天会往Github上提交代码,这个上面会有完整的项目。欢迎star,fork。GitHub:https://github.com/uweii/SHShop

我们开始:

/*
* 自定义EditText,有文字时,右边显示删除按钮
* 没有文字时,隐藏删除按钮
* */
public class ClearTextEditText extends AppCompatEditText implements View.OnFocusChangeListener, TextWatcher {
    private Drawable mClearDrawable;
    private boolean hasFocus; //控件是否有焦点
    public ClearTextEditText(Context context) {
        super(context);
        init(context, null);
    }

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

    public ClearTextEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }
    private void init(Context context, AttributeSet attr){
        mClearDrawable = getCompoundDrawables()[2];
        if(mClearDrawable == null){
            mClearDrawable = getResources().getDrawable(R.drawable.del_icon);
        }
        //drawable.setBounds :设置Drawable的边界,必须要有
        mClearDrawable.setBounds(0,0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        this.hasFocus = b;
        LogUtil.d("hasFocus: " + b);
        if (hasFocus){
            //焦点存在,而且有输入值,显示右侧删除图标
            setDrawableRightVisible(getText().length() > 0);
        }else {
            //没有焦点,隐藏删除图标
            setDrawableRightVisible(false);
            clearFocus();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP){
            if(getCompoundDrawables()[2] != null){
                //根据触摸的位置判断是否点击 右侧图标
                boolean isTouchRight = (event.getX() > (getWidth() - getTotalPaddingRight())) &&
                        (event.getX() < (getWidth() - getPaddingRight()));
                //LogUtil.d("isTouchRight: " + isTouchRight);
                if(isTouchRight){
                    setText("");
                }
            }
        }
        return super.onTouchEvent(event);
    }

    private void setDrawableRightVisible(boolean visible){
        Drawable drawableRight = visible ? mClearDrawable : null;
        //getCompoundDrawables()可以获得一个{DrawableLeft, DrawableTop, DrawableRiht, DrawableBottom}的数组。
        //getCompoundDrawables()[2]表示获取EditText的DrawableRight
        setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1], drawableRight, getCompoundDrawables()[3]);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if(mOnTextChangedListener != null){
            mOnTextChangedListener.beforeTextChanged(charSequence, i, i1, i2);
        }
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if(hasFocus){
            setDrawableRightVisible(charSequence.length() > 0);
        }
        if(mOnTextChangedListener != null){
            mOnTextChangedListener.onTextChanged(charSequence, i, i1, i2);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {
        if(mOnTextChangedListener != null){
            mOnTextChangedListener.afterTextChanged(editable);
        }
    }
    //文本改变接口监听
    public interface OnTextChangedListener{
        void beforeTextChanged(CharSequence s, int start, int count, int after);

        void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter);

        void afterTextChanged(Editable s);
    }
    private OnTextChangedListener mOnTextChangedListener;
    public void setOnTextChangedListener(OnTextChangedListener onTextChangedListener){
        this.mOnTextChangedListener = onTextChangedListener;
    }

}

现在我们就使用这个自定义控件:

 <com.up.uwei.shshop.view.ClearTextEditText
                android:id="@+id/et_search"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:layout_weight="5"
                android:background="@drawable/bg_input"
                android:drawableLeft="@drawable/ic_search"
                android:gravity="center_vertical"
                android:hint="输入搜索"
                android:textColor="@color/search_hint"
                android:textSize="15sp" />

就可以咯、

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值