目的:
有時(shí)候在做搜索功能時(shí)掸鹅,如果EditText有內(nèi)容,則在右側(cè)顯示一個(gè)刪除圖標(biāo)拦赠,點(diǎn)擊刪除圖標(biāo)刪除內(nèi)容巍沙;
當(dāng)EditText內(nèi)容為空時(shí),則隱藏刪除圖標(biāo)荷鼠。
效果圖:
edittext.gif
p.s. 這是我正在做的一個(gè)畢業(yè)設(shè)計(jì)句携,Android + JavaWeb。前端app允乐,后臺(tái)都會(huì)有矮嫉。我每天會(huì)往Github上提交代碼,這個(gè)上面會(huì)有完整的項(xiàng)目牍疏。歡迎star蠢笋,fork。GitHub:https://github.com/uweii/SHShop
/*
* 自定義EditText鳞陨,有文字時(shí)昨寞,右邊顯示刪除按鈕
* 沒有文字時(shí),隱藏刪除按鈕
* */
public class ClearTextEditText extends AppCompatEditText implements View.OnFocusChangeListener, TextWatcher {
private Drawable mClearDrawable;
private boolean hasFocus; //控件是否有焦點(diǎn)
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 :設(shè)置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){
//焦點(diǎn)存在援岩,而且有輸入值歼狼,顯示右側(cè)刪除圖標(biāo)
setDrawableRightVisible(getText().length() > 0);
}else {
//沒有焦點(diǎn),隱藏刪除圖標(biāo)
setDrawableRightVisible(false);
clearFocus();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
if(getCompoundDrawables()[2] != null){
//根據(jù)觸摸的位置判斷是否點(diǎn)擊 右側(cè)圖標(biāo)
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()可以獲得一個(gè){DrawableLeft, DrawableTop, DrawableRiht, DrawableBottom}的數(shù)組享怀。
//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);
}
}
//文本改變接口監(jiān)聽
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;
}
}
現(xiàn)在我們就使用這個(gè)自定義控件:
<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" />
就可以咯羽峰、