1.重写EditText
package ccav.xulingyun.com.testdagger2.view;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by Cantaloupe-郓 on 2017/11/27.
* 描述:
*/
public class CleanEditText extends AppCompatEditText implements View.OnFocusChangeListener, TextWatcher {
//输入框四周的图标,数组里面放置的索引0-3对应,左,上,右,下
private Drawable[] mCompoundDrawables;
//输入框右边的图标
private Drawable mCompoundRightDrawable;
//为输入框内容动态改变的监听,因为这个自定义view使用了TextWatcher,所以要在提供一个给用户使用
private OnTextChangeListener mOnTextChangeListener;
public CleanEditText(Context context) {
this(context, null);
}
public CleanEditText(Context context, AttributeSet attrs) {
//android.R.attr.editTextStyle这个很重要,要不然getCompoundDrawables()取不到值,我也不知道为啥
this(context, attrs, android.R.attr.editTextStyle);
}
public CleanEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mCompoundDrawables = getCompoundDrawables();
mCompoundRightDrawable = mCompoundDrawables[2];
setCleanIconVisible(false);
setOnFocusChangeListener(this);
}
//设置右边图标的显示与隐藏
public void setCleanIconVisible(boolean visible) {
//为空下面就没有意义,所以返回了
if (mCompoundRightDrawable == null)
return;
//不去给图标设置setBounds就可以设置图标的位置
setCompoundDrawablesWithIntrinsicBounds(mCompoundDrawables[0], mCompoundDrawables[1], visible ? mCompoundRightDrawable : null, mCompoundDrawables[3]);
}
//焦点改变事件,有焦点且内容为空 和 没有焦点都不显示图标
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
setCleanIconVisible(getText().length() > 0);
} else {
setCleanIconVisible(false);
}
}
//右边图标的点击事件
@Override
public boolean onTouchEvent(MotionEvent event) {
// Log.e("TAG", "onTouchEvent: "+getWidth());
// Log.e("TAG", "onTouchEvent: "+getCompoundPaddingRight());
// Log.e("TAG", "onTouchEvent: "+getPaddingRight());
// Log.e("TAG", "onTouchEvent: "+mCompoundRightDrawable.getIntrinsicWidth());
// Log.e("TAG", "onTouchEvent: "+event.getX());
if (event.getAction() == MotionEvent.ACTION_UP) {
if (mCompoundRightDrawable != null) {
//特别说明一下,getCompoundPaddingRight()=getPaddingRight()+mCompoundRightDrawable.getIntrinsicWidth()
if (getWidth() - getCompoundPaddingRight() < event.getX() && getWidth() - getPaddingRight() > event.getX()) {
setText("");
setCleanIconVisible(false);
return true;
}
}
}
return super.onTouchEvent(event);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (mOnTextChangeListener != null)
mOnTextChangeListener.beforeTextChanged(s, start, count, after);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
setCleanIconVisible(s.toString().length() > 0);
if (mOnTextChangeListener != null)
mOnTextChangeListener.onTextChanged(s, start, before, count);
}
@Override
public void afterTextChanged(Editable s) {
if (mOnTextChangeListener != null)
mOnTextChangeListener.afterTextChanged(s);
}
public interface OnTextChangeListener {
void beforeTextChanged(CharSequence s, int start, int count, int after);
void onTextChanged(CharSequence s, int start, int before, int count);
void afterTextChanged(Editable s);
}
public void setOnTextChangeListener(OnTextChangeListener $OnTextChangeListener) {
mOnTextChangeListener = $OnTextChangeListener;
}
//动态设置右边图标
public void setCompoundRightDrawable(Drawable drawable) {
mCompoundRightDrawable = drawable;
setCompoundDrawablesWithIntrinsicBounds(mCompoundDrawables[0], mCompoundDrawables[1], getText().length() > 0 ? mCompoundRightDrawable : null, mCompoundDrawables[3]);
}
}
2.布局文件
<ccav.xulingyun.com.testdagger2.view.CleanEditText
android:layout_width="match_parent"
android:id="@+id/mCleanEditText"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:background="#ff0000"
android:drawableLeft="@android:drawable/ic_menu_search"
android:drawableRight="@android:drawable/btn_dialog"
android:paddingRight="10dp" app:layout_constraintTop_toTopOf="parent"/>
3.在antivity中使用
mCleanEditText = findViewById(R.id.mCleanEditText);
//在这里不要去addTextChangedListener()这个方法,因为我已经在自定义view里面使用该事件,但是提供了另一个监听事件,作用和addTextChangedListener一样!
mCleanEditText.setOnTextChangeListener(new CleanEditText.OnTextChangeListener() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e("TAG", "onTextChanged: ");
}
@Override
public void afterTextChanged(Editable s) {
}
});
// 这里可以动态修改右侧的图标mCleanEditText.setCompoundRightDrawable(getResources().getDrawable(R.mipmap.ic_launcher));