带叉号的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){
//这里构造方法也很重要,不加这个很多属性不能再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) {
        //自定义的叉号图标
        mClearDrawable = getResources().getDrawable(R.drawable.ic_fork);
    }
    mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
    //默认设置隐藏图标
    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);
}

/**
 * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
 *
 * @param visible
 */
public void setClearIconVisible(boolean visible) {
    Drawable right = visible ? mClearDrawable : null;
    setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1],
            right, getCompoundDrawables()[3]);
}

/**
 * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏
 */
@Override
public void onFocusChange(View v, boolean hasFocus) {
    this.hasFocus = hasFocus;
    if (hasFocus) {
        setClearIconVisible(getText().length() > 0);
    } else {
        setClearIconVisible(false);
    }
}

/**
 * 当输入框里面内容发生变化的时候回调的方法
 */
@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) {

}

}

你可能感兴趣的:(带叉号的EditText)