(一、有清除功能的 EditText扩展组件)android 自定义组件摸索过程中的一点分享

来源:互联网 发布:淘宝零食怎么那么便宜 编辑:程序博客网 时间:2024/06/06 11:02

不觉间接触android已经有一段时间,有过两次想写东西的冲动,但是都不了了之,主要是自己没什么能说的收获和经验吧。现在尽量把自己仅有的一点干货分享给大家。

自定义带清除功能的EditView组件;该组件网上有多个版本,吸取前辈的经验;这里主要是综合了一点:

在第一个blog的基础上,加了个功能:失去焦点时,删除标记隐藏;这一点更改还是直接从下面的blog里拉来的代码汗,这里对两位前辈表示感谢!

以下的方法实现非常简单,适合已经开发差不多的项目中做一点小升级变动,我上家公司,做这种处理的时候是在代码里直接添加相应时间的监听,大家可以想象如果只改一个组件名,是多么美妙的事儿!

/*

 * zzq
 http://blog.csdn.net/aaawqqq/article/details/39851755 主要参考
 http://blog.csdn.net/zwx622/article/details/38340991  焦点优化 失去焦点隐藏图片
 * */

package chuanghe.erp.views;


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.widget.EditText;

public class ClearableEditText extends EditText {
private final String TAG = "MyEditText";
private Drawable dRight;
private Rect rBounds;


public ClearableEditText(Context paramContext) {
super(paramContext);
initEditText();
}


public ClearableEditText(Context paramContext,
AttributeSet paramAttributeSet) {
super(paramContext, paramAttributeSet);
initEditText();
}


public ClearableEditText(Context paramContext,
AttributeSet paramAttributeSet, int paramInt) {
super(paramContext, paramAttributeSet, paramInt);
initEditText();
}


// 初始化edittext 控件
private void initEditText() {
setEditTextDrawable();
addTextChangedListener(new TextWatcher() { // 对文本内容改变进行监听
@Override
public void afterTextChanged(Editable paramEditable) {
}


@Override
public void beforeTextChanged(CharSequence paramCharSequence,
int paramInt1, int paramInt2, int paramInt3) {
}


@Override
public void onTextChanged(CharSequence paramCharSequence,
int paramInt1, int paramInt2, int paramInt3) {
ClearableEditText.this.setEditTextDrawable();
}
});
}


// 控制图片的显示
public void setEditTextDrawable() {
if (getText().toString().length() == 0) {
setCompoundDrawables(null, null, null, null);
} else {
setCompoundDrawables(null, null, this.dRight, null);
}
}


@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
this.dRight = null;
this.rBounds = null;


}


/**
* 添加触摸事件 点击之后 出现 清空editText的效果
*/
@Override
public boolean onTouchEvent(MotionEvent paramMotionEvent) {
if ((this.dRight != null) && (paramMotionEvent.getAction() == 1)) {
this.rBounds = this.dRight.getBounds();
int i = (int) paramMotionEvent.getRawX();// 距离屏幕的距离
// int i = (int) paramMotionEvent.getX();//距离边框的距离
if (i > getRight() - 3 * this.rBounds.width()) {
setText("");
paramMotionEvent.setAction(MotionEvent.ACTION_CANCEL);
}
}
return super.onTouchEvent(paramMotionEvent);
}

/**
* 显示右侧X图片的

* 左上右下
*/
@Override
public void setCompoundDrawables(Drawable paramDrawable1,
Drawable paramDrawable2, Drawable paramDrawable3,
Drawable paramDrawable4) {
if (paramDrawable3 != null)
this.dRight = paramDrawable3;
super.setCompoundDrawables(paramDrawable1, paramDrawable2,
paramDrawable3, paramDrawable4);
}

@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {


} else {
setCompoundDrawables(null, null, null, null);
}
}
}



自定义组件升级 1,改下组件名;2,在android:drawableRight这个属性上定义你想要的删除样式的图片。写好类后的升级,仅仅只需前1,2两点点改动

            <chuanghe.erp.views.ClearableEditText
                android:id="@+id/loginname_login"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableRight="@drawable/delete"  
                android:background="@null"
                android:hint="@string/phonehint_login"
                android:inputType="number"
                android:textSize="16sp" />



0 0
原创粉丝点击