当前位置: 代码迷 >> Android >> Android 自定义EditText输入框 带清空旋钮
  详细解决方案

Android 自定义EditText输入框 带清空旋钮

热度:52   发布时间:2016-04-24 12:00:41.0
Android 自定义EditText输入框 带清空按钮

关注微信号:javalearns?? 随时随地学Java

或扫一扫

?

?

随时随地学Java

当用户输入字符后 ?EditText会自动在输入框的内部右侧出现删除按钮

重写EditText达到简化布局的效果
效果图:

继承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
????package?com.example.myedittexttest;
?
import?<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>.content.Context;
import?<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>.graphics.Rect;
import?<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>.graphics.drawable.Drawable;
import?<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>.text.Editable;
import?<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>.text.TextWatcher;
import?<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>.util.AttributeSet;
import?<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>.view.MotionEvent;
import?<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>.widget.EditText;
?
public?class?MyEditText?extends?EditText {
????private?final?String TAG =?"MyEditText";
????private?Drawable dRight;
????private?Rect rBounds;
?
????public?MyEditText(Context paramContext) {
????????super(paramContext);
????????initEditText();
????}
?
????public?MyEditText(Context paramContext, AttributeSet paramAttributeSet) {
????????super(paramContext, paramAttributeSet);
????????initEditText();
????}
?
????public?MyEditText(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,?intparamInt1,?int?paramInt2,?int?paramInt3) {
????????????}
?
????????????@Override
????????????public?void?onTextChanged(CharSequence paramCharSequence,?intparamInt1,?int?paramInt2,?int?paramInt3) {
????????????????MyEditText.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);
????}
}

XML布局:

?
?
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
????<RelativeLayout xmlns:<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>="http://schemas.<a title="android" href="http://www.android-study.com/jiemiansheji/590.html">android</a>.com/apk/res/<a title="android" href="http://www.android-study.com/jiemiansheji/590.html">android</a>"
????xmlns:tools="http://schemas.<a title="android" href="http://www.android-study.com/jiemiansheji/590.html">android</a>.com/tools"
????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:layout_width="match_parent"
????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:layout_height="match_parent"
????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:paddingBottom="@dimen/activity_vertical_margin"
????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:paddingLeft="@dimen/activity_horizontal_margin"
????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:paddingRight="@dimen/activity_horizontal_margin"
????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:paddingTop="@dimen/activity_vertical_margin"
????tools:context=".MainActivity"?>
?
????<com.example.myedittexttest.MyEditText
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:id="@+id/edit_text"
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:layout_width="fill_parent"
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:layout_height="50dp"
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:layout_marginTop="50dp"
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:background="#88aaff"
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:drawableRight="@drawable/edit_clear"
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:textCursorDrawable="@null"?/>
?
????<Button
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:id="@+id/button1"
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:layout_width="wrap_content"
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:layout_height="wrap_content"
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:layout_below="@+id/edit_text"
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:layout_marginTop="84dp"
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:layout_toRightOf="@+id/textView1"
????????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:text="Button"?/>
?
</RelativeLayout>

XML中的属性简介:

显示右侧的X 按钮:
?
1
2
<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:drawableRight="@drawable/edit_clear"

设置光标的颜色 ? ?设置@null ?表示光标的颜色和输入框的字体颜色相同

?
1
2
<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:textCursorDrawable="@null"

显示隐藏光标

?
1
2
3
????<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:cursorVisible="true"//显示
<a title="android"?href="http://www.android-study.com/jiemiansheji/590.html">android</a>:cursorVisible="false"//隐藏
????

有误的地方请指正

每日精进
最后神兽镇楼
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
????//┏┓   ┏┓
//┏┛┻━━━┛┻┓
//┃       ┃  
//┃   ━   ┃
//┃ ┳┛ ┗┳ ┃
//┃       ┃
//┃   ┻   ┃
//┃       ┃
//┗━┓   ┏━┛
//? ┃   ┃?? 神兽保佑        
//? ┃   ┃?? 代码无BUG!
//? ┃   ┗━━━┓
//? ┃       ┣┓
//? ┃       ┏┛
//? ┗┓┓┏━┳┓┏┛
//??? ┃┫┫ ┃┫┫
//??? ┗┻┛ ┗┻┛
????

关注微信号:javalearns?? 随时随地学Java

或扫一扫

?

?

随时随地学Java

  相关解决方案