GitOPEN's Home.

《Monkey Android》第10课TextView和EditText

Word count: 632 / Reading time: 3 min
2015/12/08 Share

通过本节课可以学习到的内容:

  • TextView的用法
  • EditText的用法
  • 登陆框实例

实例代码:

运行效果参见本课程示例App:安卓猴Demos

github地址:https://github.com/git0pen/MonkeyAndroid


TextView的用法

顾名思义,TextView就是文字视图,即显示文字的控件。

TextView的xml写法:

1
2
3
4
5
6
7
8
9
10
11
12
<TextView
android:id="@+id/tv_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:background="@android:color/holo_green_dark"
android:text="登陆Login"
android:textAllCaps="true"
android:textColor="@android:color/holo_red_dark"
android:textSize="30sp"
android:textStyle="bold"
/>

在Activity中的onCreate方法中使用它:

1
2
TextView mTextView = (TextView) findViewById(R.id.tv_login);
mTextView.setText("我是登陆框");

EditText的用法

输入文本框,它是用户和Android系统进行数据交换的窗口。

EditText有几个特点:

  • EditText是TextView的之类,所以TextView的方法和特性同样存在于EditText中;

  • 有两个常用的监听事件:addTextChangedListenersetOnEditorActionListener;

下面的代码,实现了一个只有界面的登陆框:

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1"
>


<TextView
android:id="@+id/tv_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:background="@android:color/holo_green_dark"
android:text="登陆Login"
android:textAllCaps="true"
android:textColor="@android:color/holo_red_dark"
android:textSize="30sp"
android:textStyle="bold"
/>


<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/image_view"
android:layout_margin="16dp"
android:stretchColumns="1"
>


<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<TextView
android:id="@+id/text_view_uname"
android:layout_column="0"
android:text="用户名:"
android:textSize="16sp"

/>

<EditText
android:id="@+id/et_uname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:hint="不能为空"
/>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<TextView
android:id="@+id/text_view_pwd"
android:layout_column="0"
android:text="密码:"
android:textSize="16sp"
/>

<EditText
android:id="@+id/et_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:ems="10"
android:hint="不能为空"
android:inputType="textPassword"
/>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_column="0"
android:layout_gravity="right"
android:layout_span="2"
android:orientation="horizontal"
>

<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
/>

</LinearLayout>

</TableRow>


</TableLayout>


</LinearLayout>

下课

这一节课,我们学习了TextView和EditText,它们的xml属性和函数方法还有很多,还需要你在练习的时候,耐心琢磨。


欣慰帮到你 一杯热咖啡
【奋斗的Coder!】企鹅群
【奋斗的Coder】公众号
CATALOG
  1. 1. TextView的用法
  2. 2. EditText的用法
  3. 3. 下课