天天看点

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



最终要做的项目目标:

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

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: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" />

            android:id="@+id/tv_sex"

            android:layout_marginleft="5dip"

            android:layout_torightof="@id/tv_name"

            android:text="性别"

            android:id="@+id/tv_age"

            android:layout_torightof="@id/tv_sex"

            android:text="年龄"

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

        <edittext

            android:id="@+id/et_name"

            android:layout_alignleft="@id/tv_name"

            android:layout_alignright="@id/tv_name"

            android:layout_below="@id/tv_name"

            android:singleline="true" />

        <!-- 在性别的下面 -->

            android:id="@+id/et_sex"

            android:layout_alignleft="@id/tv_sex"

            android:layout_alignright="@id/tv_sex"

            android:layout_below="@id/tv_sex"

            android:id="@+id/et_age"

            android:layout_alignleft="@id/tv_age"

            android:layout_alignright="@id/tv_age"

            android:layout_below="@id/tv_age"

            android:inputtype="number"

        <button

            android:id="@+id/btn_add_student"

            android:layout_alignbaseline="@id/et_age"

            android:layout_torightof="@id/et_age"

            android:text="添加学生"

            android:textsize="20sp" />

    </relativelayout>

    <scrollview

        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:orientation="horizontal" >

            android:id="@+id/btn_save"

            android:layout_weight="1"

            android:text="保存数据"

            android:id="@+id/btn_restore"

            android:text="恢复数据"

    </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) {

       this.name = name;

       this.sex = sex;

       this.age = age;

    public string getname() {

       return name;

    public void setname(string name) {

    public string getsex() {

       return sex;

    public void setsex(string sex) {

    public integer getage() {

       return age;

    public void setage(integer 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;

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

              toast.maketext(this, "恢复失败", 0).show();

           }

       case r.id.btn_add_student:

           addstudent();

       default:

       }

    /**

     * 从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:

                     studentlist.add(student);

              default:

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

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

    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.settext("  " + name + "  " + sex + "  " + age);

       } else {

           toast.maketext(this, "请正确输入", 0).show();

继续阅读