zoukankan      html  css  js  c++  java
  • 带删除小图标的EditText

     1 import android.content.Context;
     2 import android.graphics.Rect;
     3 import android.graphics.drawable.Drawable;
     4 import android.text.Editable;
     5 import android.text.TextWatcher;
     6 import android.util.AttributeSet;
     7 import android.util.Log;
     8 import android.view.MotionEvent;
     9 import android.widget.EditText;
    10 
    11  
    12 public class EditTextWithDel extends EditText {
    13     private final static String TAG = "EditTextWithDel";
    14     private Drawable imgInable;
    15     private Drawable imgAble;
    16     private Context mContext;
    17 
    18     public EditTextWithDel(Context context) {
    19         super(context);
    20         mContext = context;
    21         init();
    22     }
    23 
    24     public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) {
    25         super(context, attrs, defStyle);
    26         mContext = context;
    27         init();
    28     }
    29 
    30     public EditTextWithDel(Context context, AttributeSet attrs) {
    31         super(context, attrs);
    32         mContext = context;
    33         init();
    34     }
    35     
    36     private void init() {
    37         // 灰色叉号
    38         imgInable = mContext.getResources().getDrawable(R.drawable.delete_gray);
    39         // 带色叉号
    40         imgAble = mContext.getResources().getDrawable(R.drawable.delete);
    41         addTextChangedListener(new TextWatcher() {
    42             @Override
    43             public void onTextChanged(CharSequence s, int start, int before, int count) {}
    44             @Override
    45             public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    46             @Override
    47             public void afterTextChanged(Editable s) {
    48                 setDrawable();
    49             }
    50         });
    51         setDrawable();
    52     }
    53     
    54     //设置删除图片
    55     private void setDrawable() {
    56         if(length() < 1)
    57             setCompoundDrawablesWithIntrinsicBounds(null, null, imgInable, null);
    58         else
    59             setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null);
    60     }
    61     
    62      // 处理删除事件
    63     @Override
    64     public boolean onTouchEvent(MotionEvent event) {
    65         if (imgAble != null && event.getAction() == MotionEvent.ACTION_UP) {
    66             int eventX = (int) event.getRawX();
    67             int eventY = (int) event.getRawY();
    68             Log.e(TAG, "eventX = " + eventX + "; eventY = " + eventY);
    69             Rect rect = new Rect();
    70             getGlobalVisibleRect(rect);
    71             rect.left = rect.right - 50;
    72             if(rect.contains(eventX, eventY)) 
    73                 setText("");
    74         }
    75         return super.onTouchEvent(event);
    76     }
    77 
    78     @Override
    79     protected void finalize() throws Throwable {
    80         super.finalize();
    81     }
    82 
    83 }
    1 <com.EditTextWithDel
    2         android:layout_width="match_parent"
    3         android:layout_height="wrap_content"
    4         android:layout_margin="20dp"
    5         android:hint="输入"
    6         android:padding="7dp"
    7         android:singleLine="true" />

        

  • 相关阅读:
    08 linux文件检索和编辑
    Mybatis3详解(二十)——Mybatis中使用的9种设计模式(转)
    Mybatis3详解(十八)——Mybatis运行原理之Mapper接口的动态代理过程
    Mybatis3详解(十七)——Mybatis运行原理之SqlSession的构建过程
    Mybatis3详解(十六)——Mybatis运行原理之SqlSessionFactory的构建过程
    Mybatis3详解(十五)——Mybatis整合Spring框架
    Mybatis3详解(十四)——Mybatis的分页
    Mybatis3详解(十三)——Mybatis逆向工程
    Mybatis3详解(十二)——Mybatis缓存
    Mybatis3详解(十一)——延迟加载
  • 原文地址:https://www.cnblogs.com/androidsj/p/4535227.html
Copyright © 2011-2022 走看看