帶叉號的EditText

package com.ds.uilib.edittext;

import android.content.Context;
import android.graphics.Rect;
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 androidx.appcompat.widget.AppCompatEditText;

import com.ds.uilib.R;

/**

  • Created by Sai on 2018/3/27.
  • 帶刪除的EditTextView
    */

public class EditTextWithDelView extends AppCompatEditText implements View.OnFocusChangeListener,TextWatcher {
/**
* 刪除按鈕的引用
*/
private Drawable mClearDrawable;
private Context context;

/**
 * 控件是否有焦點
 */
private boolean hasFocus;

public EditTextWithDelView(Context context) {
    this(context,null);

// super(context);
// this.context = context;
// init();
}
public EditTextWithDelView(Context context, AttributeSet attrs){
//這里構(gòu)造方法也很重要,不加這個很多屬性不能再XML里面定義
this(context, attrs, android.R.attr.editTextStyle);
}

public EditTextWithDelView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}
private void init() {
    mClearDrawable = getCompoundDrawables()[2];
    if (mClearDrawable == null) {
        //自定義的叉號圖標(biāo)
        mClearDrawable = getResources().getDrawable(R.drawable.ic_fork);
    }
    mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
    //默認(rèn)設(shè)置隱藏圖標(biāo)
    setClearIconVisible(false);
    setOnFocusChangeListener(this);
    addTextChangedListener(this);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (mClearDrawable != null && event.getAction() == MotionEvent.ACTION_UP) {
        int x = (int) event.getX();
        boolean isInnerWidth = (x > (getWidth() - getTotalPaddingRight())) &&
                (x < (getWidth() - getPaddingRight()));
        Rect rect = mClearDrawable.getBounds();
        int height = rect.height();
        int y = (int) event.getY();
        int distance = (getHeight() - height) / 2;
        boolean isInnerHeight = (y > distance) && (y < (distance + height));
        if (isInnerHeight && isInnerWidth) {
            this.setText("");
        }
    }
    return super.onTouchEvent(event);
}

/**
 * 設(shè)置清除圖標(biāo)的顯示與隱藏,調(diào)用setCompoundDrawables為EditText繪制上去
 *
 * @param visible
 */
public void setClearIconVisible(boolean visible) {
    Drawable right = visible ? mClearDrawable : null;
    setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1],
            right, getCompoundDrawables()[3]);
}

/**
 * 當(dāng)ClearEditText焦點發(fā)生變化的時候,判斷里面字符串長度設(shè)置清除圖標(biāo)的顯示與隱藏
 */
@Override
public void onFocusChange(View v, boolean hasFocus) {
    this.hasFocus = hasFocus;
    if (hasFocus) {
        setClearIconVisible(getText().length() > 0);
    } else {
        setClearIconVisible(false);
    }
}

/**
 * 當(dāng)輸入框里面內(nèi)容發(fā)生變化的時候回調(diào)的方法
 */
@Override
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
    if (hasFocus) {
        setClearIconVisible(text.length() > 0);
    }
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void afterTextChanged(Editable s) {

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容