Android实现学生管理系统

Android实现学生管理系统,第1张

概述本文实例为大家分享了Android实现学生管理系统的关键性代码,供大家参考,具体内容如下

本文实例为大家分享了AndroID实现学生管理系统的关键性代码,供大家参考,具体内容如下

局部效果图:

 

实现代码:

1、布局

<?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="17" />   <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"       androID:theme="@androID:style/theme.NoTitlebar" >       <intent-filter>         <action androID:name="androID.intent.action.MAIN" />          <category androID:name="androID.intent.category.LAUNCHER" />       </intent-filter>     </activity>   </application>  </manifest> 
<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> 

2、关键代码:

package com.itheima27.sutdentmanager.entitIEs;  public class Student {    private String name;   private String sex;   private Integer age;   public Student(String name,String sex,Integer age) {     super();     this.name = name;     this.sex = sex;     this.age = age;   }   public Student() {     super();     // Todo auto-generated constructor stub   }   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 + "]";   } } 
package com.itheima27.sutdentmanager;  import java.io.fileinputStream; import java.io.fileNotFoundException; import java.io.fileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List;  import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer;  import com.itheima27.sutdentmanager.entitIEs.Student;  import androID.os.Bundle; import androID.os.Environment; import androID.app.Activity; import androID.graphics.color; import androID.text.TextUtils; import androID.util.Xml; import androID.vIEw.Menu; 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;  public class MainActivity extends Activity 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";   }    @OverrIDe   public voID onClick(VIEw v) {     switch (v.getID()) {     case R.ID.btn_save:       if(studentList.size() > 0) {         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;     }   }      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;   }      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();     }   } } 

以上就是本文的全部内容,希望对大家的学习有所帮助。

总结

以上是内存溢出为你收集整理的Android实现学生管理系统全部内容,希望文章能够帮你解决Android实现学生管理系统所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149517.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存