在应用中为了增加用户体验,方便用户操作,一般会需要一个带有清除按钮的输入框,废话不多说,直接上代码,也是自己在练习自定义view的过程吧,记录下
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import lzj.com.myview.R;
/**
* Created by Liu Zeju on 2016/6/2.
*
* 输入内容变化并且获取焦点时显示清除按钮的输入框
*/
public class ClearEditText extends EditText implements TextWatcher, View.OnFocusChangeListener {
private boolean isFoucs;
private Drawable mClearDrawable;
public ClearEditText(Context context) {
super(context);
init();
}
public ClearEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init(){
// setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom) 左上右下(0、1、2、3)
mClearDrawable = getCompoundDrawables()[2];//获取输入框右侧drawable
if(mClearDrawable == null){
mClearDrawable = getResources().getDrawable(R.drawable.icon_close);
}
mClearDrawable.setBounds(0,0,mClearDrawable.getIntrinsicWidth(),mClearDrawable.getIntrinsicHeight());
addTextChangedListener(this);//文字变化监听
setOnFocusChangeListener(this);//焦点变化监听
setClearDrawableVisible(false);//初始化不显示右侧清除按钮
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
setClearDrawableVisible(getText().length() > 0);
}
/**此处为主要核心部分(模仿按钮点击)
* 当手指抬起的位置在clean的图标的区域
* 我们将此视为进行清除操作
* getWidth():得到控件的宽度
* event.getX():抬起时的坐标(改坐标是相对于控件本身而言的)
* getTotalPaddingRight():clean的图标左边缘至控件右边缘的距离
* getPaddingRight():clean的图标右边缘至控件右边缘的距离
* getWidth() - getTotalPaddingRight()表示:
* 控件左边到clean的图标左边缘的区域
* getWidth() - getPaddingRight()表示:
* 控件左边到clean的图标右边缘的区域
* 所以这两者之间的区域刚好是clean的图标的区域
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_UP:
boolean isClean =(event.getX() > (getWidth() - getTotalPaddingRight()))&&
(event.getX() < (getWidth() - getPaddingRight()));
if (isClean) {
setText("");
}
break;
default:
break;
}
return super.onTouchEvent(event);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
isFoucs = hasFocus;
if(hasFocus){
setClearDrawableVisible(getText().length() > 0);
}else {
setClearDrawableVisible(false);
}
}
/*
*设置清除图标的显示和隐藏
*/
private void setClearDrawableVisible(boolean isVisible){
Drawable mRightDrawable;
if(isVisible){
mRightDrawable = this.mClearDrawable;
}else {
mRightDrawable = null;
}
setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1],mRightDrawable,getCompoundDrawables()[3]);
}
}