相关文章推荐
小百科
›
android_common/common/src/main/java/org/yang/common/components/PasswordEditText.java at master · Bra
奔跑的草稿纸
7 月前
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Files
master
Breadcrumbs
android_common
/
common
/
src
/
main
/
java
/
org
/
yang
/
common
/
components
/
PasswordEditText.java
Blame
Blame
Latest commit
History
History
137 lines (119 loc) · 5.13 KB
master
Breadcrumbs
android_common
/
common
/
src
/
main
/
java
/
org
/
yang
/
common
/
components
/
PasswordEditText.java
Top
File metadata and controls
Code
Blame
137 lines (119 loc) · 5.13 KB
Raw
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package
org
.
yang
.
common
.
components
;
import
android
.
content
.
Context
;
import
android
.
graphics
.
drawable
.
Drawable
;
import
android
.
support
.
v4
.
content
.
ContextCompat
;
import
android
.
support
.
v7
.
widget
.
AppCompatEditText
;
import
android
.
text
.
Editable
;
import
android
.
text
.
TextWatcher
;
import
android
.
text
.
method
.
HideReturnsTransformationMethod
;
import
android
.
text
.
method
.
PasswordTransformationMethod
;
import
android
.
text
.
method
.
TransformationMethod
;
import
android
.
util
.
AttributeSet
;
import
android
.
view
.
MotionEvent
;
import
android
.
view
.
View
;
import
android
.
view
.
View
.
OnFocusChangeListener
;
import
org
.
yang
.
common
.
R
;
public
class
PasswordEditText
extends
AppCompatEditText
implements
OnFocusChangeListener
,
TextWatcher
{
/**
* 密码按钮的引用
*/
private
Drawable
mEye
;
private
Drawable
mEyeShow
;
private
boolean
hasFoucs
;
private
String
TAG
=
getClass
().
getSimpleName
();
public
PasswordEditText
(
Context
context
) {
this
(
context
,
null
);
}
public
PasswordEditText
(
Context
context
,
AttributeSet
attrs
) {
// 这里构造方法也很重要,不加这个很多属性不能再XML里面定义
this
(
context
,
attrs
,
android
.
R
.
attr
.
editTextStyle
);
}
public
PasswordEditText
(
Context
context
,
AttributeSet
attrs
,
int
defStyle
) {
super
(
context
,
attrs
,
defStyle
);
init
();
}
private
void
init
() {
// 获取EditText的DrawableRight,假如没有设置我们就使用默认的图片,2是获得右边的图片 顺序是左上右下(0,1,2,3,)
mEye
=
getCompoundDrawables
()[
2
];
mEyeShow
=
getCompoundDrawables
()[
2
];
if
(
mEye
==
null
) {
mEye
=
ContextCompat
.
getDrawable
(
getContext
(),
R
.
mipmap
.
hide
);
}
if
(
mEyeShow
==
null
) {
mEyeShow
=
ContextCompat
.
getDrawable
(
getContext
(),
R
.
mipmap
.
show
);
}
mEyeShow
.
setBounds
(
0
,
0
,
mEye
.
getIntrinsicWidth
(),
mEye
.
getIntrinsicHeight
());
mEye
.
setBounds
(
0
,
0
,
mEye
.
getIntrinsicWidth
(),
mEye
.
getIntrinsicHeight
());
// 默认设置隐藏图标
setClearIconVisible
(
true
);
// 设置焦点改变的监听
setOnFocusChangeListener
(
this
);
// 设置输入框里面内容发生改变的监听
addTextChangedListener
(
this
);
}
/**
* 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件 当我们按下的位置 在 EditText的宽度 -
* 图标到控件右边的间距 - 图标的宽度 和 EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向就没有考虑
*/
@
Override
public
boolean
onTouchEvent
(
MotionEvent
event
) {
if
(
event
.
getAction
() ==
MotionEvent
.
ACTION_UP
) {
if
(
getCompoundDrawables
()[
2
] !=
null
) {
boolean
touchable
=
event
.
getX
() > (
getWidth
() -
getTotalPaddingRight
())
&& (
event
.
getX
() < ((
getWidth
() -
getPaddingRight
())));
if
(
touchable
) {
TransformationMethod
transformationMethod
=
getTransformationMethod
();
if
(
transformationMethod
instanceof
HideReturnsTransformationMethod
) {
setTransformationMethod
(
PasswordTransformationMethod
.
getInstance
());
}
else
if
(
transformationMethod
instanceof
PasswordTransformationMethod
) {
setTransformationMethod
(
HideReturnsTransformationMethod
.
getInstance
());
}
}
}
}
return
super
.
onTouchEvent
(
event
);
}
/**
* 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏
*/
@
Override
public
void
onFocusChange
(
View
v
,
boolean
hasFocus
) {
this
.
hasFoucs
=
hasFocus
;
if
(
hasFocus
) {
// setClearIconVisible(getText().length() > 0);
setClearIconVisible
(
true
);
}
else
{
// setClearIconVisible(false);
setClearIconVisible
(
true
);
}
}
/**
* 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
*
* @param visible
*/
protected
void
setClearIconVisible
(
boolean
visible
) {
Drawable
right
=
visible
?
mEyeShow
:
null
;
setCompoundDrawables
(
getCompoundDrawables
()[
0
],
getCompoundDrawables
()[
1
],
right
,
getCompoundDrawables
()[
3
]);
TransformationMethod
transformationMethod
=
getTransformationMethod
();
if
(
transformationMethod
instanceof
HideReturnsTransformationMethod
&&
right
!=
null
) {
setCompoundDrawables
(
getCompoundDrawables
()[
0
],
getCompoundDrawables
()[
1
],
mEye
,
getCompoundDrawables
()[
3
]);
}
/**
* 当输入框里面内容发生变化的时候回调的方法
*/
@
Override
public
void
onTextChanged
(
CharSequence
s
,
int
start
,
int
count
,
int
after
) {
if
(
hasFoucs
) {
setClearIconVisible
(
s
.
length
() >
0
);
}
}
@
Override
public
void
beforeTextChanged
(
CharSequence
s
,
int
start
,
int
count
,
int
after
) {
@
Override
public
void
afterTextChanged
(
Editable
s
) {
推荐文章