带删除Icon的EditText

带删除Icon的EditText

先上效果图:

这里写图片描述

这个控件是比较常用的。
使用了两种实现的方法,一种是继承EditText,另一种是由EditText和ImageView组成的组合控件实现。
第一种:

public class DeleteEditText extends EditText {

    private static final int EXTRA_AREA = 30;//额外点击范围
    //默认图标
    private int delIconId = R.mipmap.delete_icon;
    private Drawable delDrawalbe;
    private boolean isIconShow;


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


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

    private void init(Context context) {
        delDrawalbe = ContextCompat.getDrawable(context, delIconId);
        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) {
                if (s.length() > 0) {
                    isIconShow = true;
                    setCompoundDrawablesWithIntrinsicBounds(null, null, delDrawalbe, null);
                } else {
                    isIconShow = false;
                    setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                }
            }
        });
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isIconShow) {
            Rect bounds = getCompoundDrawables()[2].getBounds();
            int x = (int) event.getX();
           int rectX = getWidth() - bounds.width() - EXTRA_AREA - getPaddingRight();
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (x > rectX) {
                        clearText();
                         return true;
                    }
                    break;
            }

        }
        return super.onTouchEvent(event);
    }

    private void clearText() {
        setText("");
        isIconShow = false;
        //获取输入焦点
        setFocusable(true);
        setFocusableInTouchMode(true);
        requestFocus();
        findFocus();
    }

}

第二种:

public class DeleteView extends RelativeLayout {

    private ImageView ivDel;
    private EditText etText;

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

    public DeleteView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.view_delete, this);
        ivDel = (ImageView) findViewById(R.id.delete);
        etText = (EditText) findViewById(R.id.etText);
        //图标默认隐藏
        ivDel.setVisibility(GONE);
        etText.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) {
                if (s.length() > 0) {
                    ivDel.setVisibility(VISIBLE);
                } else {
                    ivDel.setVisibility(GONE);
                }
            }
        });

        ivDel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                etText.setText("");
                //获取输入焦点
                etText.setFocusable(true);
                etText.setFocusableInTouchMode(true);
                etText.requestFocus();
                etText.findFocus();
            }
        });
    }


    public EditText getEditText() {
        return etText;
    }

    public ImageView getDelIv() {
        return ivDel;
    }

}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#b2b2b2"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">

    <ImageView
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@mipmap/delete_icon"/>


    <EditText
        android:id="@+id/etText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/delete"
        android:background="@null"/>


</RelativeLayout>

第一种跟第二种相比,感觉第一种会更加灵活,直接拷贝就能使用。

如果发现有不正确的地方或者需要改良改正的话,跪求指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值