Android-EditText(自定义带删除功能的EditText)

1.说明

自定义带删除功能的EditText有两种方法,第一种是用组合视图的方法,即在一个view视图里面左侧放置一个EditText,右侧放置一个ImageView,但是这样增加了视图的层次,而且对输入内容的长度要做一定的处理。

第二种是重新定义EditText组件,增加相应的事件处理,即可达到很好的效果,效果图如下:

2.ClearEditText的JAVA类文件

/** 
 * @说明: 自定义带删除按钮的EditText 
 * 
 */  
public class ClearEditText extends EditText implements OnFocusChangeListener,  
TextWatcher {  
//EditText右侧的删除按钮  
private Drawable mClearDrawable;  
private boolean hasFoucs;  
  
public ClearEditText(Context context) {  
this(context, null);  
}  
  
public ClearEditText(Context context, AttributeSet attrs) {  
this(context, attrs, android.R.attr.editTextStyle);  
}  
  
public ClearEditText(Context context, AttributeSet attrs, int defStyle) {  
super(context, attrs, defStyle);  
init();  
}  
  
private void init() {  
// 获取EditText的DrawableRight,假如没有设置我们就使用默认的图片,获取图片的顺序是左上右下(0,1,2,3,)  
mClearDrawable = getCompoundDrawables()[2];  
if (mClearDrawable == null) {  
mClearDrawable = getResources().getDrawable(  
R.drawable.edit_delete);  
}  
  
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(),  
mClearDrawable.getIntrinsicHeight());  
// 默认设置隐藏图标  
setClearIconVisible(false);  
// 设置焦点改变的监听  
setOnFocusChangeListener(this);  
// 设置输入框里面内容发生改变的监听  
addTextChangedListener(this);  
}  
  
/* @说明:isInnerWidth, isInnerHeight为ture,触摸点在删除图标之内,则视为点击了删除图标 
 * event.getX() 获取相对应自身左上角的X坐标 
 * event.getY() 获取相对应自身左上角的Y坐标 
 * getWidth() 获取控件的宽度 
 * getHeight() 获取控件的高度 
 * getTotalPaddingRight() 获取删除图标左边缘到控件右边缘的距离 
 * getPaddingRight() 获取删除图标右边缘到控件右边缘的距离 
 * isInnerWidth: 
 *  getWidth() - getTotalPaddingRight() 计算删除图标左边缘到控件左边缘的距离 
 *  getWidth() - getPaddingRight() 计算删除图标右边缘到控件左边缘的距离 
 * isInnerHeight: 
 *  distance 删除图标顶部边缘到控件顶部边缘的距离 
 *  distance + height 删除图标底部边缘到控件顶部边缘的距离 
 */  
@Override  
public boolean onTouchEvent(MotionEvent event) {  
if (event.getAction() == MotionEvent.ACTION_UP) {  
if (getCompoundDrawables()[2] != null) {  
int x = (int)event.getX();  
int y = (int)event.getY();  
Rect rect = getCompoundDrawables()[2].getBounds();  
int height = rect.height();  
int distance = (getHeight() - height)/2;  
boolean isInnerWidth = x > (getWidth() - getTotalPaddingRight()) && x < (getWidth() - getPaddingRight());  
boolean isInnerHeight = y > distance && y <(distance + height);  
if (isInnerWidth && isInnerHeight) {  
this.setText("");  
}  
}  
}  
return super.onTouchEvent(event);  
}  
  
/** 
 * 当ClearEditText焦点发生变化的时候, 
 * 输入长度为零,隐藏删除图标,否则,显示删除图标 
 */  
@Override  
public void onFocusChange(View v, boolean hasFocus) {  
this.hasFoucs = hasFocus;  
if (hasFocus) {  
setClearIconVisible(getText().length() > 0);  
} else {  
setClearIconVisible(false);  
}  
}  
  
protected void setClearIconVisible(boolean visible) {  
Drawable right = visible ? mClearDrawable : null;  
setCompoundDrawables(getCompoundDrawables()[0],  
getCompoundDrawables()[1], right, getCompoundDrawables()[3]);  
}  
  
@Override  
public void onTextChanged(CharSequence s, int start, int count, int after) {  
if (hasFoucs) {  
setClearIconVisible(s.length() > 0);  
}  
}  
  
@Override  
public void beforeTextChanged(CharSequence s, int start, int count,  
int after) {  
  
}  
  
@Override  
public void afterTextChanged(Editable s) {  
  
}  
  
  
}  

3.引用ClearEditText的XML文件

<com.once.android_ui.selfview.ClearEditText  
android:id="@+id/user_name"  
android:layout_width="match_parent"  
android:layout_height="wrap_content"  
android:drawableLeft="@drawable/user_name"  
android:drawablePadding="7dp"  
android:hint="@string/name_tip"  
android:singleLine="true"  
android:textSize="17sp" >  
<requestFocus />  
</com.once.android_ui.selfview.ClearEditText>  
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,913评论 22 665
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,687评论 25 708
  • 我不知道为什么有那么多人觉得没办法开口说分手 到底是真的所谓“不想伤害他/她的感情” 还是“有总比没有好” 已经戳...
    耒磊阅读 208评论 0 0
  • 做这事已经有些厌倦了,这是一种怎么样的情绪?
    复苏森林阅读 158评论 0 0
  • 越狱市场介绍 与AppStore类似,提供越狱软件商店。(插曲:据说cydia之父Jay Freeman,曾经也是...
    b485c88ab697阅读 1,659评论 0 1