转载请标明出处:Smart_Arvin的专栏
在开发过程中不可避免的总会遇到比如登录注册、用户信息修改等,这时候又是不可避免的会用到EditText控件。这个控件的使用频率虽然几乎类似我们吃饭用“筷子”的频率,but能不能用出花样,用的有技术就看你有没有下功夫喽。
下面show一个可以一键清空内容的简单的EditText的实现:
代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
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
android.widget.EditText;
import
com.ktc.sczwdemo.R;
import
com.ktc.utils.ResUtils;
/**
* @Date 2016-5-13 下午2:23:23
* @Author Arvin
* @Description 可删除且带有drawableLeft的EditText
*/
public
class
EditWithDelView
extends
EditText {
private
final
static
String TAG =
"EditWithDelView"
;
private
Drawable imgDisable;
private
Drawable imgAble;
private
Context mContext;
public
EditWithDelView(Context context) {
super
(context);
mContext = context;
init();
}
public
EditWithDelView(Context context, AttributeSet attrs,
int
defStyle) {
super
(context, attrs, defStyle);
mContext = context;
init();
}
public
EditWithDelView(Context context, AttributeSet attrs) {
super
(context, attrs);
mContext = context;
init();
}
private
void
init() {
imgAble = ResUtils.getDrawable(R.drawable.delete);
addTextChangedListener(
new
TextWatcher() {
@Override
public
void
onTextChanged(CharSequence s,
int
start,
int
before,
int
count) {}
@Override
public
void
beforeTextChanged(CharSequence s,
int
start,
int
count,
int
after) {}
@Override
public
void
afterTextChanged(Editable s) {
setDrawable();
}
});
this
.setOnFocusChangeListener(
new
OnFocusChangeListener() {
@Override
public
void
onFocusChange(View v,
boolean
hasFocus) {
// TODO Auto-generated method stub
if
(hasFocus&&length()>=
1
){
setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[
0
],
null
, imgAble,
null
);
}
else
{
setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[
0
],
null
,
null
,
null
);
}
}
});
}
//根据字符长度加载提示Drawable
private
void
setDrawable() {
if
(length() <
1
){
setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[
0
],
null
,
null
,
null
);
}
else
if
(isFocused()){
setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[
0
],
null
, imgAble,
null
);
}
}
//响应触摸点击事件
@Override
public
boolean
onTouchEvent(MotionEvent event) {
if
(imgAble !=
null
&& event.getAction() == MotionEvent.ACTION_UP) {
int
eventX = (
int
) event.getRawX();
int
eventY = (
int
) event.getRawY();
Rect rect =
new
Rect();
getGlobalVisibleRect(rect);
rect.left = rect.right -
50
;
rect.top = rect.bottom -
50
;
if
(rect.contains(eventX, eventY)){
setText(
""
);
setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[
0
],
null
,
null
,
null
);
}
}
return
super
.onTouchEvent(event);
}
@Override
protected
void
finalize()
throws
Throwable {
super
.finalize();
}
}
|
里面可根据需要替换imgDisable和imgAble两个资源。
使用方法:在Xml中像普通EditText一样直接引用即可