Android自定义View-带删除和搜索图标的EditText

效果图:
这里写图片描述

直接撸代码:

public class SearchEditText extends EditText {

    private static final String TAG = "SearchEditText";

    private Drawable searchImg, delImg;

    public SearchEditText(Context context) {
        super(context);
        init();
    }

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

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

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public SearchEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        int pxDimension = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16,
                getContext().getResources().getDisplayMetrics());
        setPadding(pxDimension, 0, pxDimension, 0);
        searchImg = getContext().getResources().getDrawable(R.drawable.ic_search_red_500_24dp);
        delImg = getContext().getResources().getDrawable(R.drawable.ic_delete_forever_red_500_24dp);
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setDrawables();//内容变换后
            }
        });
        setDrawables();
    }

    private void setDrawables() {
        if (length() < 1) {
            setCompoundDrawablesWithIntrinsicBounds(searchImg, null, null, null);
        } else {
            setCompoundDrawablesWithIntrinsicBounds(searchImg, null, delImg, null);
            Drawable[] compoundDrawables = getCompoundDrawables();
            // 0 ,1 ,2, 3对应左,上,右,下
            //如果图片小可以拉伸下,代码如下
//            Rect bounds = compoundDrawables[2].getBounds();
//            int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2,
//                    getContext().getResources().getDisplayMetrics());
//            compoundDrawables[2].setBounds(bounds.left, bounds.top, bounds.right + size, bounds.bottom + size);
//            setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], compoundDrawables[2], compoundDrawables[3]);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Drawable[] compoundDrawables = getCompoundDrawables();
        if (compoundDrawables[2] != null && event.getAction() == MotionEvent.ACTION_UP) {
            int eventX = (int) event.getRawX();
            int eventY = (int) event.getRawY();
            Rect rect = new Rect();
            getGlobalVisibleRect(rect);
            rect.left = rect.right - 100;
            if (rect.contains(eventX, eventY)) {
                setText("");
            }
        }
        return super.onTouchEvent(event);
    }
}

布局文件就不贴上了,用到的图片资源也贴上,有需要的可以下载试试
这里写图片描述这里写图片描述这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值