上一篇我们详细讲了Button点击事件的原理,这里来学习EditText的基本用法。
EditText是什么
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
|
<? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".TestActivity" > < TextView android:id = "@+id/textView" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "40dp" android:background = "#cccccc" android:singleLine = "true" android:text = "Hello Activity Hello Activity Hello Activity Hello Activity" android:textSize = "20sp" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintHorizontal_bias = "0.0" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toTopOf = "parent" /> < Button android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "20dp" android:text = "Button" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/textView" /> < EditText android:id = "@+id/et_account" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "20dp" android:hint = "请输入账号" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/button" /> < EditText android:id = "@+id/et_phone" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "20dp" android:hint = "请输入手机号" android:inputType = "number" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/et_account" /> < EditText android:id = "@+id/et_password" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "20dp" android:hint = "请输入密码" android:inputType = "textPassword" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/et_phone" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
上面我们添加了三个EditText分别是帐号输入框,手机号输入框,密码输入框:
帐号输入框:
1
2
3
4
5
6
7
8
9
|
< EditText android:id = "@+id/et_account" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "20dp" android:hint = "请输入账号" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/button" /> |
其中 android:hint 是输入框中的提示文字信息,在我们输入文本之前或者清空输入的文本之后会显示出来。
手机号输入框:
1
2
3
4
5
6
7
8
9
10
|
< EditText android:id = "@+id/et_phone" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "20dp" android:hint = "请输入手机号" android:inputType = "number" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/et_account" /> |
其中 android:inputType="number" 是规定输入框内只能输入数字。
密码输入框:
1
2
3
4
5
6
7
8
9
10
|
< EditText android:id = "@+id/et_password" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "20dp" android:hint = "请输入密码" android:inputType = "textPassword" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/et_phone" /> |
其中 android:inputType="textPassword" 表示输入框输入类型为文本密码类型,可以输入任意类型的内容,但是输入之后显示的是*****。
EditText其他属性:
1
2
3
4
5
|
android:textColorHint="#95A1AA" 提示信息文字的字体颜色 android:textColor = "#ff8c00" 输入内容的字体颜色 android:singleLine="true" 单行输入,一旦设置为true,则文字不会自动换行 android:background="@null" 控件背景,@null表示背景透明 android:editable="false" 输入框不可编辑 |