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: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(); } } } |
05_学生管理系统,xml读写,布局的综合应用的更多相关文章
- EL&Filter&Listener:EL表达式和JSTL,Servlet规范中的过滤器,Servlet规范中的监听器,观察着设计模式,监听器的使用,综合案例学生管理系统
EL&Filter&Listener-授课 1 EL表达式和JSTL 1.1 EL表达式 1.1.1 EL表达式介绍 *** EL(Expression Language):表达式语言 ...
- Mybatis高级:Mybatis注解开发单表操作,Mybatis注解开发多表操作,构建sql语句,综合案例学生管理系统使用接口注解方式优化
知识点梳理 课堂讲义 一.Mybatis注解开发单表操作 *** 1.1 MyBatis的常用注解 之前我们在Mapper映射文件中编写的sql语句已经各种配置,其实是比较麻烦的 而这几年来注解开发越 ...
- java基础: ArrayList集合应用, ArrayList增删改查详解,综合java基础实现学生管理系统,
1.ArrayList 集合和数组的区别 : 共同点:都是存储数据的容器 不同点:数组的容量是固定的,集合的容量是可变的 1.1 -ArrayList的构造方法和添加方法 public ArrayLi ...
- 【IOS开发笔记02】学生管理系统
端到端的机会 虽然现在身处大公司,但是因为是内部创业团队,产品.native.前端.服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一 ...
- Qt5——从零开始的学生管理系统
Qt教程——从零开始的学生管理系统(文件) 一.项目设计 1.需求分析 记录并处理学生成绩信息. 1)添加新的学生数据: 2)根据学号对已有的学生数据进行修改: 3)根据学号删除已存在的学生信息: 4 ...
- 学生管理系统(C语言简单实现)
仅供借鉴.仅供借鉴.仅供借鉴(整理了一下大一C语言每个章节的练习题.没得题目.只有程序了) 文章目录 1 .实训名称 2.实训目的及要求 3. 源码 4.实验小结 1 .实训名称 实训12:文件 2. ...
- java版本的学生管理系统
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Frame; import java.awt.event.Ac ...
- Java+Mysql+学生管理系统
最近正在学java和数据库,想起以前写的学生管理系统,都是从网上下载,敷衍了事.闲来无事,也就自己写了一个,不过功能实现的不是很多. 开发语言:java: 开发环境:Mysql, java: 开发工具 ...
- 学生管理系统(SSM简易版)总结
之前用 Servlet + JSP 实现了一个简易版的学生管理系统,在学习了 SSM 框架之后,我们来对之前写过的项目重构一下! 技术准备 为了完成这个项目,需要掌握如下技术: Java 基础知识 前 ...
随机推荐
- sessionStorage 、localStorage 和 cookie
localStorage 和 sessionStorage HTML5 提供了两种在客户端存储数据的新方法:localStorage 和 sessionStorage: 两者都是仅在客户端(即浏览器) ...
- FJUT第四周寒假作业之第一集,临时特工?(深度优先搜索)
原网址:http://210.34.193.66:8080/vj/Contest.jsp?cid=163#P2 第一集,临时特工? TimeLimit:1000MS MemoryLimit:128M ...
- python笔记十二(匿名函数)
一.匿名函数 有些情况下,我们需要把函数当做参数传入到另外的函数中,或者是把函数作为某个函数的返回值,此时我们就可以使用匿名函数. 匿名函数的标志是lambda. >>> f = ...
- python 常用镜像
pip镜像https://pypi.tuna.tsinghua.edu.cn/simplehttps://pypi.douban.io.com/simple pip install python-qt ...
- 前端技术之_CSS详解第二天
前端技术之_CSS详解第二天 1.css基础选择器 html负责结构,css负责样式,js负责行为. css写在head标签里面,容器style标签. 先写选择器,然后写大括号,大括号里面是样式. & ...
- 高端技巧:如何使用#define定义变量
Introduction 想在源文件中定义一个跟行号有关的变量,每次都手动输入实在是太慢了,本文介绍如何使用宏定义来定义与行号有关的变量. 例如:我们想在源代码的第10行定义A_10这样的一个整形变量 ...
- J2EE进阶(十九)FileNotFoundException: http://hibernate.org/dtd/hibernate-mapping-3.0.dtd
J2EE进阶(十九)Nested exception: java.io.FileNotFoundException: http://hibernate.org/dtd/hibernate-mappin ...
- 转:LINUX/UNIX下的回车换行与WINDOWS下的区别
今天,我总算搞清楚“回车”(carriage return)和“换行”(line feed)这两个概念的来历和区别了.在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 3 ...
- 2.Cocos2d-x-3.2编写3d打飞机,项目代码总结
1.AppDelete中applicationDidFinishLaunching代码示范 2.当电话来了时,停止恢复游戏声音的代码(在AppDelegate中加入下面代码) boolAppDel ...
- nfc开发
很多Android设备已经支持NFC(近距离无线通讯技术)了.本文就以实例的方式,为大家介绍如何在Android系统中进行NFC开发. Android NFC开发环境 使用硬件:Google Nexu ...