zoukankan      html  css  js  c++  java
  • Android 实现的EditText响应drawableRight的点击事件

    1.自定义Edittext 实现右侧图标点击清空

    package com.dxw.live.view;
    
    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.support.v7.widget.AppCompatEditText;
    import android.text.TextUtils;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    
    import com.dxw.live.R;
    
    public class RightPicClickEditText extends AppCompatEditText {
        private Drawable drawableRight;
    
        public RightPicClickEditText(Context context) {
            super(context);
            drawableRight = context.getResources().getDrawable(
                    R.mipmap.edit_close);
        }
    
        public RightPicClickEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            drawableRight = context.getResources().getDrawable(
                    R.mipmap.edit_close);
        }
    
        public RightPicClickEditText(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            drawableRight = context.getResources().getDrawable(
                    R.mipmap.edit_close);
        }
    
        @Override
        protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
            if (TextUtils.isEmpty(text)) {
                setCompoundDrawablesWithIntrinsicBounds(null,
                        null, null, null);
            } else {
                setCompoundDrawablesWithIntrinsicBounds(null,
                        null, drawableRight, null);
            }
            super.onTextChanged(text, start, lengthBefore, lengthAfter);
        }
    
        // 触摸事件
        // 判断DrawableLeft/DrawableRight是否被点击
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // 触摸状态
            if (event.getAction() == MotionEvent.ACTION_UP) {
                // 监听DrawableLeft
    //            if (onDrawableRightListener != null) {
                    // 判断DrawableLeft是否被点击
                    Drawable drawableRight = getCompoundDrawables()[2];
                    // 当按下的位置 < 在EditText的到左边间距+图标的宽度+Padding
    //                if (drawableLeft != null && event.getRawX() 在EditText的到右边间距 - 图标的宽度 - Padding
                    if (drawableRight != null) {
                        setText("");
                        setCompoundDrawablesWithIntrinsicBounds(null,
                                null, null, null);
                        // 执行DrawableRight点击事件
    //                    onDrawableRightListener.onDrawableRightClick();
                    }
    //            }
            }
            return super.onTouchEvent(event);
        }
    
        // 定义一个DrawableLeft点击事件接口
        public interface OnDrawableLeftListener {
            void onDrawableLeftClick();
        }
    
        private OnDrawableLeftListener onDrawableLeftListener;
    
        public void setOnDrawableLeftListener(OnDrawableLeftListener onDrawableLeftListener) {
            this.onDrawableLeftListener = onDrawableLeftListener;
        }
    
        // 定义一个DrawableRight点击事件接口
        public interface OnDrawableRightListener {
            void onDrawableRightClick();
        }
    
        private OnDrawableRightListener onDrawableRightListener;
    
        public void setOnDrawableRightListener(OnDrawableRightListener onDrawableRightListener) {
            this.onDrawableRightListener = onDrawableRightListener;
        }
    }
  • 相关阅读:
    [译]C++如何切分字符串
    Reverse Words in a String
    excel 获取中文拼音首字母
    jquery 获取css position的值
    excel 截取单元格部分内容(从指定位置截取)
    excel 根据单元格内容自动调整列宽
    excel 如何为列添加指定内容(字符串)
    android_handler(二)
    &quot;Simple Factory&quot; vs &quot;Factory Method&quot; vs &quot;Abstract Factory&quot; vs &quot;Reflect&quot;
    HDU 5074 Hatsune Miku 2014 Asia AnShan Regional Contest dp(水
  • 原文地址:https://www.cnblogs.com/huihuizhang/p/10849157.html
Copyright © 2011-2022 走看看