zoukankan      html  css  js  c++  java
  • 带有×的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);
        }
    }
  • 相关阅读:
    贪吃蛇 666
    安装postgresql
    linux CentOS6.5 yum安装mysql 5.6
    centos--git搭建之Gogs安装
    查看mysql 默认端口号和修改端口号
    centos之mysql安装配置使用
    流媒体服务器SRS部署
    vue用webpack打包时引入es2015插件
    log4j2的log输出到tomcat/logs目录下及使用(转)
    log4j2的配置文件log4j2.xml笔记
  • 原文地址:https://www.cnblogs.com/zzw1994/p/5032009.html
Copyright © 2011-2022 走看看