【移動應用開發(fā)技術(shù)】Android版如何實現(xiàn)學生管理系統(tǒng)_第1頁
【移動應用開發(fā)技術(shù)】Android版如何實現(xiàn)學生管理系統(tǒng)_第2頁
【移動應用開發(fā)技術(shù)】Android版如何實現(xiàn)學生管理系統(tǒng)_第3頁
【移動應用開發(fā)技術(shù)】Android版如何實現(xiàn)學生管理系統(tǒng)_第4頁
【移動應用開發(fā)技術(shù)】Android版如何實現(xiàn)學生管理系統(tǒng)_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

【移動應用開發(fā)技術(shù)】Android版如何實現(xiàn)學生管理系統(tǒng)

<uses-permission

android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<uses-permission

android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><LinearLayout

xmlns:android="/apk/res/android"

xmlns:tools="/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="學生管理系統(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="添加學生"/>

</LinearLayout>

<!--

ScrollView只可以包裹一個控件

-->

<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="恢復數(shù)據(jù)"/>

</LinearLayout>

</LinearLayout>/upload/information/20200623/125/124468.jpgpackage

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();

=

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)

{

=

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;

}

}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;

//學生列表控件

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:

//添加學生

//取出數(shù)據(jù)

String

name

=

et_name.getText().toString();

String

sex

=

et_sex.getText().toString();

String

age

=

et_age.getText().toString();

//控制臺打印數(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,

"請重新輸入",

0).show();

break;

}

//封裝成Student實體

Student

student

=

new

Student(name,

sex,

Integer.valueOf(age));

//添加到學生列表中

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:

//恢復數(shù)據(jù)

//

恢復數(shù)據(jù)之前,

需要把集合中和LinearLayout中的數(shù)據(jù)全部清空

studentList.clear();

//

把線性布局中所有的控件全部移除

llStudentGroup.removeAllViews();

//從文件中讀取數(shù)據(jù)

List<Student>

readStudentList

=

readStudentList();

//

把取出回來的數(shù)據(jù),

一條一條的添加到學生列表中

for

(Student

stu

:

readStudentList)

{

addToStudentList(stu);

}

break;

default:

break;

}

}

private

List<Student>

readStudentList()

{

List<Student>

studentList

=

null;

//

構(gòu)建一個XmlPullParser解析器對象

XmlPullParser

parser

=

Xml.newPullParser();

//

指定要解析的文件

try

{

parser.setInput(new

FileInputStream(cacheFile),

"utf-8");

//

開始解析:

獲取EventType解析事件,

循環(huán)去判斷事件

int

eventType

=

parser.getEventType();

Student

student

=

null;

while(eventType!=XmlPullParser.END_DOCUMENT){

//

當前的事件類型不等于結(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();

//

取下一個事件類型

}

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);

//得到一個序列化對象

XmlSerializer

serializer

=

Xml.newSerializer();

try

{

//指定輸出流

serializer.setOutput(new

FileOutputStream(cacheFile),

"utf-8");

//寫start_document

serializer.startDocument("utf-8",

true);

//

寫<students>

serializer.startTag(null,

"students");

for

(Student

student

:

studentList)

{

Log.e(TAG,

student.toString());

//

寫<student>

serializer.startTag(null,

"student");

//

寫<name>

serializer.startTag(null,

"name");

serializer.text(student.getName());

//

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論