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

android:name="com.itheima27.sutdentmanager.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

3 编写布局文件activity_main.xml

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

4 编写Student实体

package com.itheima27.sutdentmanager.entities;

public class Student {

private String name;

private String sex;

private Integer age;

public Student() {

super();

}

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

super();

this.name = name;

this.sex = sex;

this.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;

}

@Override

public String toString() {

return "Student [name=" + name + ", sex=" + sex + ", age=" + 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;

public class MainActivity extends ActionBarActivity 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";

}

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

}

}

/**

* 从xml中读出Student数据

* @return

*/

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;

}

/**

* 恢复student的List列表

*/

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

}

}

}

最新文章

  1. Java随笔三
  2. 烂泥:php5.6源码安装与apache集成
  3. 关于在win7内集成usb3.0驱动。
  4. Facebook内部分享:26个高效工作的小技巧
  5. 字符串js编码转换成实体html编码的方法(防范XSS攻击)
  6. OD调试篇5--如何应对OD使用中的一些问题
  7. Intel baytrail-t support Linux?
  8. Oracle入门4-REF Cursor
  9. SharePoint 2013 引发类型为“System.ArgumentException”的异常。 參数名: encodedValue
  10. git 实际操作
  11. 【webpack】流行的前端模块化工具webpack初探
  12. 初学Python——面向对象(二)
  13. 获取AWR报告
  14. Rsync常见错误和问题
  15. LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium
  16. &quot;去QE化&quot;的思考
  17. vmware 已将该虚拟机配置为使用 64 位客户机操作系统。但是,无法执行 64 位操作。
  18. sql中的duplicate的使用
  19. module.exports与exports的联系与区别
  20. Android 使用 Gradle 多渠道打包

热门文章

  1. 用go实现常用算法与数据结构——队列(queue)
  2. MySQL之sql文件的导入导出
  3. Request JSON
  4. python用openpyxl操作excel
  5. Gradle 1.12用户指南翻译——第五十一章. 发布构件
  6. RAP在线接口管理统计部署
  7. 28 自定义View侧滑栏
  8. Android EditText在ScrollView中被输入法遮挡
  9. git中status指令总是提示内容被修改的解决
  10. Scikit-learn:scikit-learn快速教程及实例