DeletableEditText.java
public class DeletableEditText extends EditText {
private Drawable mRightDrawable;
private boolean isHasFocus;
public DeletableEditText(Context context) {
this(context, null);
}
public DeletableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DeletableEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
// getCompoundDrawables:获取组合图片的数组,包括上下左右四个方位
// Returns drawables for the left, top, right, and bottom borders.
Drawable[] drawables = this.getCompoundDrawables();
// 取得right位置的Drawable
// 即我们在布局文件中设置的android:drawableRight
mRightDrawable = drawables[2];
// 设置焦点变化的监听
this.setOnFocusChangeListener(new FocusChangeListenerImpl());
// 设置EditText文字变化的监听
this.addTextChangedListener(new TextWatcherImpl());
// 初始化时让右边clean图标不可见
setClearDrawableVisible(false);
}
/**
* 当手指抬起的位置在clean的图标的区域 我们将此视为进行清除操作 </br>
* getWidth():得到控件的宽度 </br>
* event.getX():抬起时的坐标(改坐标是相对于控件本身而言的) </br>
* getTotalPaddingRight():clean的图标左边缘至控件右边缘的距离 </br>
* getPaddingRight():clean的图标右边缘至控件右边缘的距离 于是: </br>
* getWidth() - getTotalPaddingRight()表示: 控件左边到clean的图标左边缘的区域 </br>
* getWidth() - getPaddingRight()表示: 控件左边到clean的图标右边缘的区域 </br>
* 所以这两者之间的区域刚好是clean的图标的区域 </br>
*/
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
boolean isClean = (event.getX() > (getWidth() - getTotalPaddingRight()))
&& (event.getX() < (getWidth() - getPaddingRight()));
if (isClean) {
setText("");
}
break;
default:
break;
}
return super.onTouchEvent(event);
}
private class FocusChangeListenerImpl implements OnFocusChangeListener {
@Override
public void onFocusChange(View v, boolean hasFocus) {
isHasFocus = hasFocus;
if (isHasFocus) {
setClearDrawableVisible(getText().toString().length() >= 1);
} else {
setClearDrawableVisible(false);
}
}
}
/** 当输入结束后判断是否显示右边clean的图标 */
private class TextWatcherImpl implements TextWatcher {
@Override
public void afterTextChanged(Editable s) {
String text = getText().toString();
setClearDrawableVisible(text.length() >= 1);
if (text.length() >= 1 && text.trim().length() <= 0) {
DeletableEditText.this.startAnimation(shakeAnimation(5));
DeletableEditText.this.setText("");
setClearDrawableVisible(text.trim().length() >= 1);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
/** 隐藏或者显示右边clean的图标 */
protected void setClearDrawableVisible(boolean isVisible) {
Drawable rightDrawable;
if (isVisible) {
rightDrawable = mRightDrawable;
} else {
rightDrawable = null;
}
// 使用代码设置该控件left, top, right, and bottom处的图标
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], rightDrawable,
getCompoundDrawables()[3]);
}
/** 显示一个动画,以提示用户输入 */
public void setShakeAnimation() {
this.startAnimation(shakeAnimation(5));
}
/***
* CycleTimes动画重复的次数
*
* @param CycleTimes
* 重复的次数
* @return
*/
public Animation shakeAnimation(int CycleTimes) {
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 10);
translateAnimation.setInterpolator(new CycleInterpolator(CycleTimes));
translateAnimation.setDuration(1000);
return translateAnimation;
}
}
使用:
<com.wxcily.xunbo2.DeletableEditText
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_edittext" android:drawableLeft="@drawable/ic_edittext_search"
android:drawablePadding="6dp" android:drawableRight="@drawable/ic_edittext_delete"
android:hint="请输入您要搜索的影片..."
android:paddingBottom="7dp"
android:paddingLeft="9dp"
android:paddingRight="8dp"
android:paddingTop="7dp"
android:singleLine="true"
android:textColor="#ffffff"
android:textCursorDrawable="@null"
android:textSize="16dp" />
注意,一定要记得在布局文件中加上:android:drawableRight=”@drawable/ic_edittext_delete”这个属性哦。
坚持写blog,每天开开心心coding!!!!!!