本文实例为大家分享了android实现学生管理系统的关键性代码,供大家参考,具体内容如下
局部效果图:
实现代码:
1、布局
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package = "com.itheima27.sutdentmanager" android:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "17" /> <uses-permission android:name= "android.permission.write_external_storage" /> <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= "com.itheima27.sutdentmanager.mainactivity" android:label= "@string/app_name" android:theme= "@android:style/theme.notitlebar" > <intent-filter> <action android:name= "android.intent.action.main" /> <category android:name= "android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest> |
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
|
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:background= "@android:color/white" android:orientation= "vertical" > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_horizontal" android:layout_margintop= "5dip" android:text= "学生管理系统" android:textcolor= "#99ccff" android:textsize= "23sp" /> <relativelayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_margintop= "5dip" android:padding= "5dip" > <textview android:id= "@+id/tv_name" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:paddingleft= "15dip" android:paddingright= "15dip" android:text= "姓名" android:textsize= "18sp" /> <textview android:id= "@+id/tv_sex" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "5dip" android:layout_torightof= "@id/tv_name" android:paddingleft= "15dip" android:paddingright= "15dip" android:text= "性别" android:textsize= "18sp" /> <textview android:id= "@+id/tv_age" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "5dip" android:layout_torightof= "@id/tv_sex" android:paddingleft= "15dip" android:paddingright= "15dip" android:text= "年龄" android:textsize= "18sp" /> <edittext android:id= "@+id/et_name" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignleft= "@id/tv_name" android:layout_alignright= "@id/tv_name" android:layout_below= "@id/tv_name" android:singleline= "true" /> <edittext android:id= "@+id/et_sex" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignleft= "@id/tv_sex" android:layout_alignright= "@id/tv_sex" android:layout_below= "@id/tv_sex" android:singleline= "true" /> <edittext android:id= "@+id/et_age" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignleft= "@id/tv_age" android:layout_alignright= "@id/tv_age" android:layout_below= "@id/tv_age" android:inputtype= "number" android:singleline= "true" /> <button android:id= "@+id/btn_add_student" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignbaseline= "@id/et_age" android:layout_torightof= "@id/et_age" android:text= "添加学生" android:textsize= "20sp" /> </relativelayout> <scrollview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_weight= "1" > <linearlayout android:id= "@+id/ll_student_list" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:layout_margin= "1dip" android:orientation= "vertical" android:padding= "5dip" > </linearlayout> </scrollview> <linearlayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_margintop= "5dip" android:orientation= "horizontal" > <button android:id= "@+id/btn_save" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_weight= "1" android:text= "保存数据" android:textsize= "20sp" /> <button android:id= "@+id/btn_restore" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_weight= "1" android:text= "恢复数据" android:textsize= "20sp" /> </linearlayout> </linearlayout> |
2、关键代码:
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
|
package com.itheima27.sutdentmanager.entities; public class student { private string name; private string sex; private integer age; public student(string name, string sex, integer age) { super (); this .name = name; this .sex = sex; this .age = age; } public student() { super (); // todo auto-generated constructor stub } public string getname() { return name; } public void setname(string name) { this .name = name; } public string getsex() { return sex; } public void setsex(string sex) { this .sex = sex; } public integer getage() { return age; } public void setage(integer age) { this .age = age; } @override public string tostring() { return "student [name=" + name + ", sex=" + sex + ", age=" + age + "]" ; } } |
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
package com.itheima27.sutdentmanager; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.util.arraylist; import java.util.list; import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserexception; import org.xmlpull.v1.xmlserializer; import com.itheima27.sutdentmanager.entities.student; import android.os.bundle; import android.os.environment; import android.app.activity; import android.graphics.color; import android.text.textutils; import android.util.xml; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.edittext; import android.widget.linearlayout; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity implements onclicklistener { private edittext etname; private edittext etsex; private edittext etage; private linearlayout llstudentlist; private list<student> studentlist; private string filepath; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); init(); } private void init() { etname = (edittext) findviewbyid(r.id.et_name); etsex = (edittext) findviewbyid(r.id.et_sex); etage = (edittext) findviewbyid(r.id.et_age); llstudentlist = (linearlayout) findviewbyid(r.id.ll_student_list); findviewbyid(r.id.btn_save).setonclicklistener( this ); findviewbyid(r.id.btn_restore).setonclicklistener( this ); findviewbyid(r.id.btn_add_student).setonclicklistener( this ); studentlist = new arraylist<student>(); filepath = environment.getexternalstoragedirectory().getpath() + "/student.xml" ; } @override public void onclick(view v) { switch (v.getid()) { case r.id.btn_save: if (studentlist.size() > 0 ) { if (savestudent2local()) { toast.maketext( this , "保存成功" , 0 ).show(); } else { toast.maketext( this , "保存失败" , 0 ).show(); } } else { toast.maketext( this , "当前没有数据" , 0 ).show(); } break ; case r.id.btn_restore: if (restorestudentfromlocal()) { toast.maketext( this , "恢复成功" , 0 ).show(); } else { toast.maketext( this , "恢复失败" , 0 ).show(); } break ; case r.id.btn_add_student: addstudent(); break ; default : break ; } } private boolean restorestudentfromlocal() { try { xmlpullparser parser = xml.newpullparser(); parser.setinput( new fileinputstream(filepath), "utf-8" ); int eventtype = parser.geteventtype(); studentlist.clear(); student student = null ; string nodename = null ; while (eventtype != xmlpullparser.end_document) { nodename = parser.getname(); switch (eventtype) { case xmlpullparser.start_tag: if ( "student" .equals(nodename)) { student = new student(); } else if ( "name" .equals(nodename)) { student.setname(parser.nexttext()); } else if ( "sex" .equals(nodename)) { student.setsex(parser.nexttext()); } else if ( "age" .equals(nodename)) { student.setage(integer.valueof(parser.nexttext())); } break ; case xmlpullparser.end_tag: if ( "student" .equals(nodename)) { studentlist.add(student); } break ; default : break ; } eventtype = parser.next(); } refreshstudentlist(); return true ; } catch (exception e) { e.printstacktrace(); } return false ; } private void refreshstudentlist() { llstudentlist.removeallviews(); textview childview; for (student student : studentlist) { childview = new textview( this ); childview.settextsize( 23 ); childview.settextcolor(color.black); childview.settext( " " + student.getname() + " " + student.getsex() + " " + student.getage()); llstudentlist.addview(childview); } } private boolean savestudent2local() { try { xmlserializer serializer = xml.newserializer(); serializer.setoutput( new fileoutputstream(filepath), "utf-8" ); serializer.startdocument( "utf-8" , true ); serializer.starttag( null , "infos" ); for (student stu : studentlist) { serializer.starttag( null , "student" ); serializer.starttag( null , "name" ); serializer.text(stu.getname()); serializer.endtag( null , "name" ); serializer.starttag( null , "sex" ); serializer.text(stu.getsex()); serializer.endtag( null , "sex" ); serializer.starttag( null , "age" ); serializer.text(string.valueof(stu.getage())); serializer.endtag( null , "age" ); serializer.endtag( null , "student" ); } serializer.endtag( null , "infos" ); serializer.enddocument(); return true ; } catch (exception e) { e.printstacktrace(); } return false ; } private void addstudent() { string name = etname.gettext().tostring(); string sex = etsex.gettext().tostring(); string age = etage.gettext().tostring(); if (!textutils.isempty(name) && !textutils.isempty(sex) && !textutils.isempty(age)) { studentlist.add( new student(name, sex, integer.valueof(age))); textview childview = new textview( this ); childview.settextsize( 23 ); childview.settextcolor(color.black); childview.settext( " " + name + " " + sex + " " + age); llstudentlist.addview(childview); } else { toast.maketext( this , "请正确输入" , 0 ).show(); } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助。