Browse Source

ClearEditText

niusongtao@dl-cg.com 5 years ago
parent
commit
8ac7304a1a

+ 115 - 0
base_library/src/main/java/com/mgtech/base_library/custom/ClearEditText.java

@@ -0,0 +1,115 @@
+package com.mgtech.base_library.custom;
+
+import android.annotation.SuppressLint;
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.graphics.drawable.Drawable;
+import android.text.Editable;
+import android.text.TextWatcher;
+import android.util.AttributeSet;
+import android.view.MotionEvent;
+import android.view.View;
+import android.widget.EditText;
+
+import androidx.appcompat.widget.AppCompatEditText;
+
+import com.mgtech.base_library.R;
+
+/**
+ * @ProjectName: BaseLibrary
+ * @Package: com.mgtech.base_library.custom
+ * @ClassName: ClearEditText
+ * @Description: 带一键清除的EditText
+ * @Author: Administrator
+ * @CreateDate: 2020/7/20 9:01
+ * @UpdateUser: 更新者:Administrator
+ * @UpdateDate: 2020/7/20 9:01
+ * @UpdateRemark: 更新说明:
+ * @Version: 1.0
+ */
+@SuppressLint("AppCompatCustomView")
+public class ClearEditText extends EditText implements View.OnFocusChangeListener, TextWatcher {
+
+    private Drawable mClearDrawable;
+    private boolean hasFocus;
+
+    public ClearEditText(Context context) {
+        this(context, null);
+    }
+
+    public ClearEditText(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        init(context, attrs);
+    }
+
+    public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+        init(context, attrs);
+    }
+
+    private void init(Context context, AttributeSet attrs) {
+        // getCompoundDrawables() Returns drawables for the left(0), top(1), right(2) and bottom(3)
+        mClearDrawable = getCompoundDrawables()[2]; // 获取drawableRight
+        if (mClearDrawable == null) {
+            TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.ClearEditText);
+            // 如果为空,即没有设置drawableRight,则使用R.mipmap.close这张图片
+            mClearDrawable = typedArray.getDrawable(R.styleable.ClearEditText_clear_image);
+            typedArray.recycle();
+        }
+        mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
+        setOnFocusChangeListener(this);
+        addTextChangedListener(this);
+        // 默认隐藏图标
+        setDrawableVisible(false);
+    }
+
+    /**
+     * 我们无法直接给EditText设置点击事件,只能通过按下的位置来模拟clear点击事件
+     * 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效
+     */
+    @Override
+    public boolean onTouchEvent(MotionEvent event) {
+        if (event.getAction() == MotionEvent.ACTION_UP) {
+            if (getCompoundDrawables()[2] != null) {
+                int start = getWidth() - getTotalPaddingRight() + getPaddingRight(); // 起始位置
+                int end = getWidth(); // 结束位置
+                boolean available = (event.getX() > start) && (event.getX() < end);
+                if (available) {
+                    this.setText("");
+                }
+            }
+        }
+        return super.onTouchEvent(event);
+    }
+
+    @Override
+    public void onFocusChange(View v, boolean hasFocus) {
+        this.hasFocus = hasFocus;
+        if (hasFocus && getText().length() > 0) {
+            setDrawableVisible(true); // 有焦点且有文字时显示图标
+        } else {
+            setDrawableVisible(false);
+        }
+    }
+
+    @Override
+    public void onTextChanged(CharSequence s, int start, int count, int after) {
+        if (hasFocus) {
+            setDrawableVisible(s.length() > 0);
+        }
+    }
+
+    @Override
+    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+    }
+
+    @Override
+    public void afterTextChanged(Editable s) {
+    }
+
+    protected void setDrawableVisible(boolean visible) {
+        Drawable right = visible ? mClearDrawable : null;
+        setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
+    }
+
+}

BIN
base_library/src/main/res/drawable-xxhdpi/base_ic_clear.png


+ 5 - 0
base_library/src/main/res/values/attrs.xml

@@ -122,4 +122,9 @@
         <attr name="cursorDrawable" format="reference" />
     </declare-styleable>
 
+
+    <declare-styleable name="ClearEditText">
+        <attr name="clear_image" format="reference"/>
+    </declare-styleable>
+
 </resources>