05_学生管理系统,xml读写,布局的综合应用

前端之家收集整理的这篇文章主要介绍了05_学生管理系统,xml读写,布局的综合应用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

  1. 最终要做的项目目标:


2
、编写Android清单文件AndroidManifest.xml

<?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="19" />

<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

"com.itheima27.sutdentmanager.MainActivity"

"@string/app_name" >

<intent-filter>

<action "android.intent.action.MAIN" />

<category "android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

3 编写布局文件activity_main.xml

<LinearLayout android:layout_width="match_parent"

android:layout_height=android:background="@android:color/white"

android:orientation="vertical" >

<TextView

"wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="5dip"

android:text="学生管理系统"

android:textColor="#99CCFF"

android:textSize="23sp"/>

<RelativeLayout

"fill_parent"

"5dip"

android:padding="5dip">

<TextView

android:id="@+id/tv_name"

android:paddingLeft="15dip"

android:paddingRight="15dip"

姓名"18sp" />

<"@+id/tv_sex"

android:layout_marginLeft=android:layout_toRightOf="@id/tv_name"

性别"@+id/tv_age"

"@id/tv_sex"

年龄"18sp" />

<!-- 在姓名的下面 -->

<EditText

"@+id/et_name"

android:layout_alignLeft=android:layout_alignRight=android:layout_below=android:singleLine="true" />

在性别的下面"@+id/et_sex"

"true" />

<EditText

"@+id/et_age"

"@id/tv_age"

android:inputType="number"

Button

"@+id/btn_add_student"

android:layout_alignBaseline="@id/et_age"

"@id/et_age"

添加学生"20sp" />

</RelativeLayout>

<ScrollView

"fill_parent"

"wrap_content"

android:layout_weight="1" >

<LinearLayout

"@+id/ll_student_list"

"fill_parent"

android:layout_margin="1dip"

"vertical"

"5dip" >

</LinearLayout>

</ScrollView>

<"horizontal" >

<"@+id/btn_save"

"1"

保存数据"20sp" />

<"@+id/btn_restore"

恢复数据LinearLayout>

</LinearLayout>

4 编写Student实体

package com.itheima27.sutdentmanager.entities;

public class Student {

private String name;

sex;

private Integer age;

public Student() {

super();

}

public Student(String name,String sex,Integer age) {

super();

this.name = name;

sex = sex;

age = age;

}

public String getName() {

return name;

}

void setName(String name) {

name = name;

}

public String getSex() {

sex;

}

void setSex(String sex) {

sex = sex;

}

public Integer getAge() {

age;

}

void setAge(Integer age) {

age = age;

}

@Override

public String toString() {

return "Student [name=" + name + ",sex=" + sex + + age + "]";

}

}

5 编写MainActivity

package com.itheima27.sutdentmanager;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.ArrayList;

import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlSerializer;

import android.graphics.Color;

import android.os.Bundle;

import android.os.Environment;

import android.support.v7.app.ActionBarActivity;

import android.text.TextUtils;

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 com.itheima27.sutdentmanager.entities.Student;

class MainActivity extends ActionBarActivity implements OnClickListener {

private EditText etName;

etSex;

etAge;

private LinearLayout llStudentList;

private List<Student> studentList;

private String filePath;

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(btn_add_student).setOnClickListener(this);

studentList = new ArrayList<Student>();

filePath = Environment.getExternalStorageDirectory().getPath() + "/student.xml";

}

void onClick(View v) {

switch (v.getId()) {

case R.id.btn_save:

if(studentList.size() > 0) {

//将信息写到xml文件

if(saveStudent2Local()) {

Toast.makeText(this,"保存成功",0).show();

} else {

Toast.makeText(保存失败

}

} else {

Toast.makeText(当前没有数据

}

break;

btn_restore:

if(restoreStudentFromLocal()) {

Toast.makeText(恢复成功

} 恢复失败

}

break;

btn_add_student:

addStudent();

default:

break;

}

}

/**

* xml中读出Student数据

* @return

*/

boolean restoreStudentFromLocal() {

try {

XmlPullParser parser = Xml.newPullParser();

parser.setInput(new FileInputStream(filePath),255); font-family: Consolas; font-size: 11pt; mso-font-kerning: 0pt;">"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 "name".equals(nodeName)) {

student.setName(parser.nextText());

} "sex".equals(nodeName)) {

student.setSex(parser.nextText());

} "age".equals(nodeName)) {

student.setAge(Integer.valueOf(parser.nextText()));

}

break;

END_TAG:

"student".equals(nodeName)) {

studentList.add(student);

}

default:

break;

}

eventType = parser.next();

}

refreshStudentList();

return true;

} catch (Exception e) {

e.printStackTrace();

}

false;

}

恢复studentList列表

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

}

}

boolean saveStudent2Local() {

try {

XmlSerializer serializer = Xml.newSerializer();

serializer.setOutput(new FileOutputStream("utf-8");

serializer.startDocument("utf-8",true);

serializer.startTag(null,255); font-family: Consolas; font-size: 11pt; mso-font-kerning: 0pt;">"infos");

for (Student stu : studentList) {

serializer.startTag("student");

serializer.startTag("name");

serializer.text(stu.getName());

serializer.endTag("name");

serializer.startTag("sex");

serializer.text(stu.getSex());

serializer.endTag("sex");

serializer.startTag("age");

serializer.text(String.valueOf(stu.getAge()));

serializer.endTag("age");

serializer.endTag("student");

}

serializer.endTag("infos");

serializer.endDocument();

false;

}

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 = " + name + " + sex + " + age);

llStudentList.addView(childView);

} else {

Toast.makeText(请正确输入

}

}

}

原文链接:https://www.f2er.com/xml/296598.html

猜你在找的XML相关文章