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);
    }
}

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


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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,931评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,698评论 0 17
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,190评论 4 61
  • 今天,哦,不,算起来应该是昨天了,驾照终于拿到手了!走在回家的路上,跟神经病似的会偷着乐,傻笑竟然还!时不时的会掏...
    BelindaRen阅读 1,176评论 0 0
  • 工具:Powerpoint 2016任务:让你的PPT动起来尺寸:16:9 从小我就对会动的东西感兴趣,所以那时候...
    刚刚的钢阅读 3,736评论 19 11