Android版學(xué)生管理系統(tǒng)
用戶(hù)可以輸入姓名、性別、年齡三個(gè)字段,通過(guò)點(diǎn)擊添加學(xué)生按鈕,將學(xué)生信息展示到開(kāi)始為空的ScrollView控件中,ScrollView控件只能包裹一個(gè)控件,我這里包裹的是LinearLayout。點(diǎn)擊保存數(shù)據(jù)按鈕將數(shù)據(jù)通過(guò)XmlSerializer對(duì)象將數(shù)據(jù)保存到sd卡中,當(dāng)點(diǎn)擊恢復(fù)數(shù)據(jù)按鈕時(shí)將sd卡文件中的數(shù)據(jù)讀取出來(lái)回顯到ScrollView中。大概功能就是這樣的,下面我們來(lái)看看具體的代碼吧。
因?yàn)橐x寫(xiě)文件,所以要在清單文件中添加兩個(gè)權(quán)限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
首先,我們畫(huà)出UI界面,具體代碼和效果如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dip"
android:textSize="28sp"
android:textColor="#D5F2F4"
android:text="學(xué)生管理系統(tǒng)" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:orientation="horizontal">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="18sp"
android:textColor="#DAD5D9"
android:text="姓名"/>
<EditText
android:id="@+id/et_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="18sp"
android:textColor="#DAD5D9"
android:text="性別"/>
<EditText
android:id="@+id/et_sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="18sp"
android:textColor="#DAD5D9"
android:text="年齡"/>
<EditText
android:id="@+id/et_age"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:singleLine="true"/>
</LinearLayout>
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:textColor="@android:color/black"
android:textSize="20sp"
android:text="添加學(xué)生"/>
</LinearLayout>
<!-- ScrollView只可以包裹一個(gè)控件 -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_marginTop="5dip">
<LinearLayout
android:id="@+id/ll_student_group"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip">
<Button
android:id="@+id/btn_save"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="20sp"
android:textColor="@android:color/black"
android:text="保存數(shù)據(jù)"/>
<Button
android:id="@+id/btn_restore"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="20sp"
android:textColor="@android:color/black"
android:text="恢復(fù)數(shù)據(jù)"/>
</LinearLayout>
</LinearLayout>
效果圖:

之后我們要建立一個(gè)student的domain,就包括了三個(gè)字段,name、age、sex,為了方便觀(guān)看,我也將Student代碼也全部貼出來(lái):
package cn.yzx.studentmanageros.domain;
public class Student {
private String name;
private String sex;
private Integer age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, String sex, Integer age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
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;
}
}
之后,也是最主要的,activity的實(shí)現(xiàn)文件,我這里直接將代碼貼出來(lái),因?yàn)樽⑨尩暮芮宄?/p>
package cn.yzx.studentmanageros;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.Format;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.util.Xml;
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;
import cn.yzx.studentmanageros.domain.Student;
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
private EditText et_name;
private EditText et_sex;
private EditText et_age;
private LinearLayout llStudentGroup; //學(xué)生列表控件
private List<Student> studentList;
private File cacheFile = new File(Environment.getExternalStorageDirectory(), "student.xml");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
studentList = new ArrayList<Student>();
et_name = (EditText) findViewById(R.id.et_name);
et_sex = (EditText) findViewById(R.id.et_sex);
et_age = (EditText) findViewById(R.id.et_age);
llStudentGroup = (LinearLayout) findViewById(R.id.ll_student_group);
findViewById(R.id.btn_add).setOnClickListener(this);
findViewById(R.id.btn_save).setOnClickListener(this);
findViewById(R.id.btn_restore).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add: //添加學(xué)生
//取出數(shù)據(jù)
String name = et_name.getText().toString();
String sex = et_sex.getText().toString();
String age = et_age.getText().toString();
//控制臺(tái)打印數(shù)據(jù)
Log.e(TAG, "name="+name+",sex="+sex+",age="+age);
//判斷是否有數(shù)據(jù)為空
if(TextUtils.isEmpty(name) || TextUtils.isEmpty(sex) || TextUtils.isEmpty(age)){
Toast.makeText(this, "請(qǐng)重新輸入", 0).show();
break;
}
//封裝成Student實(shí)體
Student student = new Student(name, sex, Integer.valueOf(age));
//添加到學(xué)生列表中
addToStudentList(student);
break;
case R.id.btn_save: //保存數(shù)據(jù)
boolean isSuccess = saveStudentList();
if(isSuccess){
Toast.makeText(this, "保存成功", 0).show();
}else {
Toast.makeText(this, "保存失敗", 0).show();
}
break;
case R.id.btn_restore: //恢復(fù)數(shù)據(jù)
// 恢復(fù)數(shù)據(jù)之前, 需要把集合中和LinearLayout中的數(shù)據(jù)全部清空
studentList.clear();
// 把線(xiàn)性布局中所有的控件全部移除
llStudentGroup.removeAllViews();
//從文件中讀取數(shù)據(jù)
List<Student> readStudentList = readStudentList();
// 把取出回來(lái)的數(shù)據(jù), 一條一條的添加到學(xué)生列表中
for (Student stu : readStudentList) {
addToStudentList(stu);
}
break;
default:
break;
}
}
private List<Student> readStudentList() {
List<Student> studentList = null;
// 構(gòu)建一個(gè)XmlPullParser解析器對(duì)象
XmlPullParser parser = Xml.newPullParser();
// 指定要解析的文件
try {
parser.setInput(new FileInputStream(cacheFile), "utf-8");
// 開(kāi)始解析: 獲取EventType解析事件, 循環(huán)去判斷事件
int eventType = parser.getEventType();
Student student = null;
while(eventType!=XmlPullParser.END_DOCUMENT){
// 當(dāng)前的事件類(lèi)型不等于結(jié)束的document, 繼續(xù)循環(huán)
String tagName = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if("students".equals(tagName)){//<students>
studentList = new ArrayList<Student>();
}else if ("student".equals(tagName)) {//<student>
student = new Student();
}else if ("name".equals(tagName)) {
student.setName(parser.nextText());
}else if ("sex".equals(tagName)) {
student.setSex(parser.nextText());
}else if("age".equals(tagName)){
student.setAge(Integer.valueOf(parser.nextText()));
}
break;
case XmlPullParser.END_TAG:
if("student".equals(tagName)){
studentList.add(student);
}
break;
default:
break;
}
eventType = parser.next(); // 取下一個(gè)事件類(lèi)型
}
return studentList;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private boolean saveStudentList() {
String path = cacheFile.getPath();
Log.e(TAG, "保存文件的路徑:"+path);
//得到一個(gè)序列化對(duì)象
XmlSerializer serializer = Xml.newSerializer();
try {
//指定輸出流
serializer.setOutput(new FileOutputStream(cacheFile), "utf-8");
//寫(xiě)start_document
serializer.startDocument("utf-8", true);
// 寫(xiě)<students>
serializer.startTag(null, "students");
for (Student student : studentList) {
Log.e(TAG, student.toString());
// 寫(xiě)<student>
serializer.startTag(null, "student");
// 寫(xiě)<name>
serializer.startTag(null, "name");
serializer.text(student.getName());
// 寫(xiě)</name>
serializer.endTag(null, "name");
// 寫(xiě)<sex>
serializer.startTag(null, "sex");
serializer.text(student.getSex());
// 寫(xiě)</sex>
serializer.endTag(null, "sex");
// 寫(xiě)<age>
serializer.startTag(null, "age");
serializer.text(String.valueOf(student.getAge()));
// 寫(xiě)</age>
serializer.endTag(null, "age");
// 寫(xiě)</student>
serializer.endTag(null, "student");
}
// 寫(xiě)</students>
serializer.endTag(null, "students");
//寫(xiě)end_document
serializer.endDocument();
return true;
} catch(Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 把給定的學(xué)生添加到學(xué)生列表中
* @param student
*/
private void addToStudentList(Student student) {
// 把學(xué)生信息添加到集合中, 為了方便呆會(huì)去保存
studentList.add(student);
//創(chuàng)建一個(gè)TextView對(duì)象,并設(shè)置其內(nèi)容
TextView tv = new TextView(this);
String text = " "+student.getName()+" "+student.getSex()+" "+student.getAge();
tv.setText(text);
tv.setTextSize(18);
tv.setTextColor(Color.BLACK);
// 把TextView添加到學(xué)生列表中
llStudentGroup.addView(tv);
}
}
大家可以試試,有什么問(wèn)題可以留言討論,本人才學(xué)android,希望各位大神指正。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義Dialog實(shí)現(xiàn)文字動(dòng)態(tài)加載效果
這篇文章主要為大家詳細(xì)介紹了Android自定義Dialog實(shí)現(xiàn)文字動(dòng)態(tài)加載效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Flutter實(shí)現(xiàn)增強(qiáng)版的頁(yè)面懸浮按鈕的示例代碼
Flutter?自帶的?FloatingActionButton?為我們提供了一個(gè)懸浮在頂部的按鈕,這個(gè)按鈕始終在最頂層,因此可以做一些快捷的操作。本文就來(lái)和大家詳細(xì)聊聊2023-01-01
Android 5秒學(xué)會(huì)使用手勢(shì)解鎖功能
本文講述的是一個(gè)手勢(shì)解鎖的庫(kù),可以定制顯示隱藏宮格點(diǎn)、路徑、并且?guī)в行【艑m格顯示圖,和震動(dòng)!讓你學(xué)會(huì)使用這個(gè)簡(jiǎn)單,高效的庫(kù),好了,具體內(nèi)容詳情大家通過(guò)本文學(xué)習(xí)吧2017-12-12
詳解Android WebView監(jiān)聽(tīng)console錯(cuò)誤信息
這篇文章主要介紹了Android WebView監(jiān)聽(tīng)console錯(cuò)誤信息,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Android2.3實(shí)現(xiàn)Android4.0風(fēng)格EditText的方法
這篇文章主要介紹了Android2.3實(shí)現(xiàn)Android4.0風(fēng)格EditText的方法,涉及Android界面布局及控件調(diào)用的相關(guān)技巧,需要的朋友可以參考下2016-03-03
android底部彈出iOS7風(fēng)格對(duì)話(huà)選項(xiàng)框(QQ對(duì)話(huà)框)--第三方開(kāi)源之IOS_Dialog_Library
這篇文章主要介紹了android底部彈出iOS7風(fēng)格對(duì)話(huà)選項(xiàng)框(QQ對(duì)話(huà)框)--第三方開(kāi)源--IOS_Dialog_Library的相關(guān)資料,需要的朋友可以參考下2015-11-11
iOS開(kāi)發(fā)中TableView類(lèi)似QQ分組的折疊與展開(kāi)效果
這篇文章主要介紹了iOS開(kāi)發(fā)中TableView類(lèi)似QQ分組的折疊與展開(kāi)效果,其實(shí)要做這個(gè)效果我先想到的是在tableView中再嵌套多個(gè)tableView。下面通過(guò)本文給大家分享實(shí)現(xiàn)思路,需要的朋友可以參考下2016-12-12

