zoukankan      html  css  js  c++  java
  • 3.12

    带删除按钮的EditText

    public class EditTextWithDel extends EditText {
    
        private final static String TAG = "EditTextWithDel";
        private Drawable imgInable;
        private Drawable imgAble;
        private Context mContext;
    
        public EditTextWithDel(Context context) {
            super(context);
            mContext = context;
            init();
        }
    
        public EditTextWithDel(Context context, AttributeSet attrs) {
            super(context, attrs);
            mContext = context;
            init();
        }
    
        public EditTextWithDel(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            mContext = context;
            init();
        }
    
        private void init() {
            imgInable = mContext.getResources().getDrawable(R.drawable.delete_gray);
            addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
    
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    setDrawable();
                }
            });
            setDrawable();
        }
    
        // 设置删除图片
        private void setDrawable() {
            if (length() < 1)
                setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            else
                setCompoundDrawablesWithIntrinsicBounds(null, null, imgInable, null);
        }
    
        // 处理删除事件
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (imgInable != null && event.getAction() == MotionEvent.ACTION_UP) {
                int eventX = (int) event.getRawX();
                int eventY = (int) event.getRawY();
                Log.e(TAG, "eventX = " + eventX + "; eventY = " + eventY);
                Rect rect = new Rect();
                getGlobalVisibleRect(rect);
                rect.left = rect.right - 100;
                if (rect.contains(eventX, eventY))
                    setText("");
            }
            return super.onTouchEvent(event);
        }
        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }
  • 相关阅读:
    POJ 1651:Multiplication Puzzle(区间DP)
    POJ 2955:Brackets(区间DP)
    LightOJ 1422:Halloween Costumes(区间DP入门)
    Gym 101257G:24(尺取)
    Codeforces 777D:Cloud of Hashtags(水题)
    Gym 101257B:2Trees(DFS+思维)
    Codeforces 777C:Alyona and Spreadsheet(思维)
    Codeforces 776C:Molly's Chemicals(思维)
    HDU-3440 House Man
    BZOJ-1202 狡猾的商人
  • 原文地址:https://www.cnblogs.com/20193898liufa/p/14908287.html
Copyright © 2011-2022 走看看