• 带有×的EditText


    代码:

    EditTextWithDel.java(直接复制):

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

    activity_main.xml:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical">
     6 
     7     <com.sunday.customs.EditTextWithDel
     8         android:layout_width="match_parent"
     9         android:layout_height="wrap_content"
    10         android:layout_margin="20dp"
    11         android:hint="输入"
    12         android:padding="7dp"
    13         android:singleLine="true" />
    14 
    15     <com.sunday.customs.EditTextWithDel
    16         android:layout_width="match_parent"
    17         android:layout_height="wrap_content"
    18         android:layout_margin="20dp"
    19         android:hint="输入"
    20         android:padding="7dp"
    21         android:singleLine="true" />
    22 
    23 </LinearLayout>

    MainActivity.java:

    package com.sunday.customs;
    
    
    import com.example.customs.R;
    
    import android.os.Bundle;
    import android.app.Activity;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }
  • 相关阅读:
    黄聪:PowerPoint设计编辑动画的时候图层隐藏和显示问题
    黄聪:如何在Windows上安裝BeautifulSoup
    黄聪:二、如何通过URL获取其他网页源代码内容(火狐插件扩展开发教程)
    黄聪:2006 MySQL server has gone away错误,最大值溢出解决办法 mysql max_allowed_packet 查询和修改
    黄聪:解决python中文处理乱码,先要弄懂“字符”和“字节”的差别
    ExtAspNet v2.2.1
    ExtAspNet v2.3.2
    [原创]全球首款不使用ViewState的Asp.Net2.0控件库
    [原创]从程序员角度分析安徽电信HTTP劫持的无耻行径 草根的暂时胜利
    [原创]从程序员角度分析安徽电信HTTP劫持的无耻行径 – 之深度分析
  • 原文地址:https://www.cnblogs.com/zzw1994/p/5032009.html
Copyright © 2020-2023  润新知