本文章已收录于:
版权声明:本文为博主原创文章,未经博主允许不得转载。
一、效果图
二、特点
1.简单。只有一个不到100行的类,且无任何依赖,也没有自定义属性
2.高效。没有使用LinearLayout包含EditText+ImageView的组合形式实现,仅仅只有一个继承EditText的自定义view,减少了布局的嵌套和view的数量
3.易用。看我代码中的调用就知道多简单了
- <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>
<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代码
- <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>
<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
我的同类文章
http://blog.csdn.net
- •Android获取本应用内存占用的方法2017-02-27
- •BaseExpandableListAdapter封装2016-08-14
- •Android图片压缩2016-07-30
- •Android自定义相机定点聚焦2016-07-26
- •Android中GLSurfaceView截图2016-06-08
- •Android如何将软键盘回车换成搜索等按钮,EditText中imeOptions属性的使用2016-06-05
- •Android根据Uri获得其在文件系统中的路径2017-02-16
- •EventBus3.0配置及使用2016-08-03
- •Android光线传感器获取光线强弱。LightSensorManager封装类2016-07-26
- •Intent/Bundle传递Bitmap的时候失败甚至崩溃2016-06-13
- •解析Android中应用程序文件存储用得到的一些文件路径2016-06-07