目的:
有时候在做搜索功能时,如果EditText有内容,则在右侧显示一个删除图标,点击删除图标删除内容;
当EditText内容为空时,则隐藏删除图标。
效果图:
p.s. 这是我正在做的一个毕业设计,Android + JavaWeb。前端app,后台都会有。我每天会往Github上提交代码,这个上面会有完整的项目。欢迎star,fork。GitHub:https://github.com/uweii/SHShop
/*
* 自定义EditText,有文字时,右边显示删除按钮
* 没有文字时,隐藏删除按钮
* */
public class ClearTextEditText extends AppCompatEditText implements View.OnFocusChangeListener, TextWatcher {
private Drawable mClearDrawable;
private boolean hasFocus; //控件是否有焦点
public ClearTextEditText(Context context) {
super(context);
init(context, null);
}
public ClearTextEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public ClearTextEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attr){
mClearDrawable = getCompoundDrawables()[2];
if(mClearDrawable == null){
mClearDrawable = getResources().getDrawable(R.drawable.del_icon);
}
//drawable.setBounds :设置Drawable的边界,必须要有
mClearDrawable.setBounds(0,0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
setOnFocusChangeListener(this);
addTextChangedListener(this);
}
@Override
public void onFocusChange(View view, boolean b) {
this.hasFocus = b;
LogUtil.d("hasFocus: " + b);
if (hasFocus){
//焦点存在,而且有输入值,显示右侧删除图标
setDrawableRightVisible(getText().length() > 0);
}else {
//没有焦点,隐藏删除图标
setDrawableRightVisible(false);
clearFocus();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
if(getCompoundDrawables()[2] != null){
//根据触摸的位置判断是否点击 右侧图标
boolean isTouchRight = (event.getX() > (getWidth() - getTotalPaddingRight())) &&
(event.getX() < (getWidth() - getPaddingRight()));
//LogUtil.d("isTouchRight: " + isTouchRight);
if(isTouchRight){
setText("");
}
}
}
return super.onTouchEvent(event);
}
private void setDrawableRightVisible(boolean visible){
Drawable drawableRight = visible ? mClearDrawable : null;
//getCompoundDrawables()可以获得一个{DrawableLeft, DrawableTop, DrawableRiht, DrawableBottom}的数组。
//getCompoundDrawables()[2]表示获取EditText的DrawableRight
setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1], drawableRight, getCompoundDrawables()[3]);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(mOnTextChangedListener != null){
mOnTextChangedListener.beforeTextChanged(charSequence, i, i1, i2);
}
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(hasFocus){
setDrawableRightVisible(charSequence.length() > 0);
}
if(mOnTextChangedListener != null){
mOnTextChangedListener.onTextChanged(charSequence, i, i1, i2);
}
}
@Override
public void afterTextChanged(Editable editable) {
if(mOnTextChangedListener != null){
mOnTextChangedListener.afterTextChanged(editable);
}
}
//文本改变接口监听
public interface OnTextChangedListener{
void beforeTextChanged(CharSequence s, int start, int count, int after);
void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter);
void afterTextChanged(Editable s);
}
private OnTextChangedListener mOnTextChangedListener;
public void setOnTextChangedListener(OnTextChangedListener onTextChangedListener){
this.mOnTextChangedListener = onTextChangedListener;
}
}
现在我们就使用这个自定义控件:
<com.up.uwei.shshop.view.ClearTextEditText
android:id="@+id/et_search"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_weight="5"
android:background="@drawable/bg_input"
android:drawableLeft="@drawable/ic_search"
android:gravity="center_vertical"
android:hint="输入搜索"
android:textColor="@color/search_hint"
android:textSize="15sp" />
就可以咯、