EditText之有删除按钮的输入框

这里写图片描述

public class CusEditView extends android.support.v7.widget.AppCompatEditText {

    //EditText右侧的删除按钮
    private Drawable mDeleteDrawable;
    private Context mContext;

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

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

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


    private void init(Context context) {
        this.mContext=context;
        mDeleteDrawable = getCompoundDrawables()[2];
        if (mDeleteDrawable == null) {
            mDeleteDrawable = getResources().getDrawable(
                    R.drawable.delete);
        }
        //设置mDeleteDrawable占的面积
        mDeleteDrawable.setBounds(0, 0, mDeleteDrawable.getIntrinsicWidth(),
                mDeleteDrawable.getIntrinsicHeight());
        // 默认设置隐藏图标
        setClearIconVisible(true);
        //输入框背景
        setBackgroundResource(R.drawable.search_edit_bg);
        //输入字离输入框的边界
        setPadding(dip2px(5),dip2px(3),dip2px(5),dip2px(3));
        // 设置输入框里面内容发生改变的监听
        addTextChangedListener(watcher);
    }


    private TextWatcher watcher = 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) {
            if (s.length()>0){
                setClearIconVisible(true);
            }else {
                setClearIconVisible(false);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    /**
     * 得到删除按钮的区域
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {

                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
                        && (event.getX() < ((getWidth() - getPaddingRight())));
                if (touchable) {
                    this.setText("");
                }
            }
        }

        return super.onTouchEvent(event);
    }

    private void setClearIconVisible(boolean visible) {
        Drawable right = visible ? mDeleteDrawable : null;
        setCompoundDrawables(getCompoundDrawables()[0],
                getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    private   int dip2px(float dpValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

使用的例子

        <你的类的包名.CusEditView
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:singleLine="true" />

输入框的背景我自定义的一个xml。

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#FFFFFF"/>
    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />
    <stroke android:width="1dp" android:color="#cccccc"/>

</shape>

猜你喜欢

转载自blog.csdn.net/sinat_32089827/article/details/78062234