欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现学生管理系统

程序员文章站 2024-02-28 08:07:46
本文实例为大家分享了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(); 
  } 
 } 
} 

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