ClearEditText,自带清除功能的EditText

ClearEditText,自带清除功能的EditText

标签: androidClearEditText自定义view
1296人阅读 评论(1) 收藏 举报
本文章已收录于:
分类:

一、效果图



二、特点

1.简单。只有一个不到100行的类,且无任何依赖,也没有自定义属性

2.高效。没有使用LinearLayout包含EditText+ImageView的组合形式实现,仅仅只有一个继承EditText的自定义view,减少了布局的嵌套和view的数量

3.易用。看我代码中的调用就知道多简单了

  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="com.afei.myedittext.MainActivity">  
  11.   
  12.     <com.afei.myedittext.ClearEditText  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"/>  
  15. </RelativeLayout></span>  
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.afei.myedittext.MainActivity">

    <com.afei.myedittext.ClearEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout></span>

三、ClearEditText代码

  1. <span style="font-size:14px;">package com.afei.myedittext;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.drawable.Drawable;  
  5. import android.text.Editable;  
  6. import android.text.TextWatcher;  
  7. import android.util.AttributeSet;  
  8. import android.view.MotionEvent;  
  9. import android.view.View;  
  10. import android.widget.EditText;  
  11.   
  12. public class ClearEditText extends EditText implements View.OnFocusChangeListener, TextWatcher {  
  13.   
  14.     private Drawable mClearDrawable;  
  15.     private boolean hasFocus;  
  16.   
  17.     public ClearEditText(Context context) {  
  18.         this(context, null);  
  19.     }  
  20.   
  21.     public ClearEditText(Context context, AttributeSet attrs) {  
  22.         super(context, attrs);  
  23.         init();  
  24.     }  
  25.   
  26.     public ClearEditText(Context context, AttributeSet attrs, int defStyle) {  
  27.         super(context, attrs, defStyle);  
  28.         init();  
  29.     }  
  30.   
  31.     private void init() {  
  32.         // getCompoundDrawables() Returns drawables for the left(0), top(1), right(2) and bottom(3)  
  33.         mClearDrawable = getCompoundDrawables()[2]; // 获取drawableRight  
  34.         if (mClearDrawable == null) {  
  35.             // 如果为空,即没有设置drawableRight,则使用R.mipmap.close这张图片  
  36.             mClearDrawable = getResources().getDrawable(R.mipmap.close);  
  37.         }  
  38.         mClearDrawable.setBounds(00, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());  
  39.         setOnFocusChangeListener(this);  
  40.         addTextChangedListener(this);  
  41.         // 默认隐藏图标  
  42.         setDrawableVisible(false);  
  43.     }  
  44.   
  45.     /** 
  46.      * 我们无法直接给EditText设置点击事件,只能通过按下的位置来模拟clear点击事件 
  47.      * 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效 
  48.      */  
  49.     @Override  
  50.     public boolean onTouchEvent(MotionEvent event) {  
  51.         if (event.getAction() == MotionEvent.ACTION_UP) {  
  52.             if (getCompoundDrawables()[2] != null) {  
  53.                 int start = getWidth() - getTotalPaddingRight() + getPaddingRight(); // 起始位置  
  54.                 int end = getWidth(); // 结束位置  
  55.                 boolean available = (event.getX() > start) && (event.getX() < end);  
  56.                 if (available) {  
  57.                     this.setText("");  
  58.                 }  
  59.             }  
  60.         }  
  61.         return super.onTouchEvent(event);  
  62.     }  
  63.   
  64.     @Override  
  65.     public void onFocusChange(View v, boolean hasFocus) {  
  66.         this.hasFocus = hasFocus;  
  67.         if (hasFocus && getText().length() > 0) {  
  68.             setDrawableVisible(true); // 有焦点且有文字时显示图标  
  69.         } else {  
  70.             setDrawableVisible(false);  
  71.         }  
  72.     }  
  73.   
  74.     @Override  
  75.     public void onTextChanged(CharSequence s, int start, int count, int after) {  
  76.         if (hasFocus) {  
  77.             setDrawableVisible(s.length() > 0);  
  78.         }  
  79.     }  
  80.   
  81.     @Override  
  82.     public void beforeTextChanged(CharSequence s, int start, int count, int after) {  
  83.     }  
  84.   
  85.     @Override  
  86.     public void afterTextChanged(Editable s) {  
  87.     }  
  88.   
  89.     protected void setDrawableVisible(boolean visible) {  
  90.         Drawable right = visible ? mClearDrawable : null;  
  91.         setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);  
  92.     }  
  93.   
  94. }</span>  
<span style="font-size:14px;">package com.afei.myedittext;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

public class ClearEditText extends EditText implements View.OnFocusChangeListener, TextWatcher {

    private Drawable mClearDrawable;
    private boolean hasFocus;

    public ClearEditText(Context context) {
        this(context, null);
    }

    public ClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        // getCompoundDrawables() Returns drawables for the left(0), top(1), right(2) and bottom(3)
        mClearDrawable = getCompoundDrawables()[2]; // 获取drawableRight
        if (mClearDrawable == null) {
            // 如果为空,即没有设置drawableRight,则使用R.mipmap.close这张图片
            mClearDrawable = getResources().getDrawable(R.mipmap.close);
        }
        mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
        // 默认隐藏图标
        setDrawableVisible(false);
    }

    /**
     * 我们无法直接给EditText设置点击事件,只能通过按下的位置来模拟clear点击事件
     * 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {
                int start = getWidth() - getTotalPaddingRight() + getPaddingRight(); // 起始位置
                int end = getWidth(); // 结束位置
                boolean available = (event.getX() > start) && (event.getX() < end);
                if (available) {
                    this.setText("");
                }
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        this.hasFocus = hasFocus;
        if (hasFocus && getText().length() > 0) {
            setDrawableVisible(true); // 有焦点且有文字时显示图标
        } else {
            setDrawableVisible(false);
        }
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after) {
        if (hasFocus) {
            setDrawableVisible(s.length() > 0);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    protected void setDrawableVisible(boolean visible) {
        Drawable right = visible ? mClearDrawable : null;
        setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
    }

}</span>

四、注意事项

1.图标你可以通过在xml中Android:drawableRight=""指定,当然如果你不指定我没呢就会使用一个默认图标,这个图标需要事先准备,毕竟ic_launcher太丑了

2.图标默认显示在右侧,如果你的需求很古怪的话可以自己修改相应代码轻松实现(修改getCompoundDrawables()[]对应的数组下标)

3.DropEditText整个控件的高度不要太小,否则文字或者图片会显示不全,这是EditText都会有的问题

4.例如我的DropEditText使用的高度为“wrap_content",但是图片如果较大的话当drawable显示的时候就会撑高DropEditText的高度,所以你的图片高度应该适中,我使用的图片是60*60的,放置在xxhdpi下效果就很好


五、其他资源及git地址

Git地址:  http://git.oschina.net/afei_/MyEditText

close.png图片:


1
0
 
 
我的同类文章
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值