本文实例讲述了android开发之textview使用intent传递信息,实现注册界面功能。分享给大家供大家参考,具体如下:
使用intent在活动间传递值
首先是 mainactuvity 活动(注册界面 写完个人信息点击注册 )
跳转到 in 活动 (通过 intent 获得 mainactivity 中的信息 )
效果图如下:
mainactivity 实现:
java代码:
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
|
public class home extends appcompatactivity { //用于存放个人注册信息 edittext user_name ; edittext user_code ; edittext user_year ; edittext user_birth ; edittext user_phone ; //注册按钮 点击跳转 button button01 ; public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //显示manlayout user_name = (edittext) findviewbyid(r.id.ed_name); user_code = (edittext) findviewbyid(r.id.ed_code); user_year = (edittext) findviewbyid(r.id.ed_year); user_birth = (edittext) findviewbyid(r.id.ed_birth); user_phone = (edittext) findviewbyid(r.id.ed_phone); button01 = (button) findviewbyid(r.id.bn_01); //通过 intent 实现活动间的信息传递 button01.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { intent intent01 = new intent(home. this ,in. class ); intent01.putextra( "name" ,user_name.gettext().tostring()); intent01.putextra( "code" ,user_code.gettext().tostring()); intent01.putextra( "year" ,user_year.gettext().tostring()); intent01.putextra( "birth" ,user_birth.gettext().tostring()); intent01.putextra( "phone" ,user_phone.gettext().tostring()); startactivity(intent01); } }); } } |
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
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
|
<tablelayout android:id= "@+id/root" xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http://schemas.android.com/apk/res-auto" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:stretchcolumns= "1" > <tablerow> <textview android:id= "@+id/tv_name" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "用户名" android:textsize= "16sp" /> <edittext android:id= "@+id/ed_name" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:hint= "请填写登陆账号" android:selectallonfocus= "true" /> </tablerow> <tablerow> <textview android:id= "@+id/tv_code" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "密码" android:textsize= "16sp" /> <!--android:inputtype= "numberpassword" 表示只能接受数字密码--> <edittext android:id= "@+id/ed_code" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:inputtype= "numberpassword" /> </tablerow> <tablerow> <textview android:id= "@+id/tv_year" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "年龄" android:textsize= "16sp" /> <!--android:inputtype= "numberpassword" 表示是数值输入框--> <edittext android:id= "@+id/ed_year" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:inputtype= "number" /> </tablerow> <tablerow> <textview android:id= "@+id/tv_birth" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "生日" android:textsize= "16sp" /> <!--android:inputtype= "numberpassword" 表示日期输入框--> <edittext android:id= "@+id/ed_birth" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:inputtype= "date" /> </tablerow> <tablerow> <textview android:id= "@+id/tv_phone" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "电话号码" android:textsize= "16sp" /> <!--android:inputtype= "numberpassword" 表示电话号码输入框--> <edittext android:id= "@+id/ed_phone" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:selectallonfocus= "true" android:inputtype= "phone" /> </tablerow> <button android:id= "@+id/bn_01" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "注册" /> </tablelayout> |
in 活动:
java代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class in extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_in); //获得mainactivity传进来的数据 intent intent01 = getintent(); //放置传入的信息 textview textview01 = (textview) findviewbyid(r.id.in_tv_01); textview01.settext( intent01.getstringextra( "name" ) + "\n" + intent01.getstringextra( "code" ) + "\n" + intent01.getstringextra( "year" ) + "\n" + intent01.getstringextra( "birth" ) + "\n" + intent01.getstringextra( "phone" ) ); } } |
xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?xml version= "1.0" encoding= "utf-8" ?> <android.support.constraint.constraintlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http://schemas.android.com/apk/res-auto" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ".in" > <!-- //放置前一个活动传递进来的信息--> <textview android:id= "@+id/in_tv_01" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> </android.support.constraint.constraintlayout> |
希望本文所述对大家android程序设计有所帮助。
原文链接:https://blog.csdn.net/qq_43377749/article/details/83933287