Android realizes student management system _android

Source: Internet
Author: User
Tags gettext

This example for you to share the Android implementation of the student management system of key code for your reference, the specific content as follows

Local Effect Chart:

Implementation code:

1, layout

<?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 "> <u SES-SDK android:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" Andro Id.permission.WRITE_EXTERNAL_STORAGE "/> <application android:allowbackup=" true "android:icon=" @drawab Le/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity A Ndroid:name= "com.itheima27.sutdentmanager.MainActivity" android:label= "@string/app_name" Android:theme= "@and Roid:style/theme.notitlebar "> <intent-filter> <action android:name=" Android.intent.action.MAI 
    N "/> <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:lay Out_gravity= "Center_horizontal" android:layout_margintop= "5dip" "android:text=" Student Management System "android:textcolor=" #9 9CCFF "android:textsize=" 23sp "/> <relativelayout android:layout_width=" Fill_parent "Android:lay 
      out_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" a ndroid:paddingleft= "15dip" android:paddingright= "15dip" android:text= "name" android:textsize= "18SP"/&G 
 
    T <textview andRoid:id= "@+id/tv_sex" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Androi d:layout_marginleft= "5dip" android:layout_torightof= "@id/tv_name" android:paddingleft= "15dip" Android :p addingright= "15dip" android:text= "Sex" 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:paddingr ight= "15dip" android:text= "Age" android:textsize= "18sp"/> <edittext android:id= "@+id/et_" Name "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "Android:layout_alignle ft= "@id/tv_name" android:layout_alignright= "@id/tv_name" android:layout_below= "@id/tv_name" android:s 
Ingleline= "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=" numb Er "android:singleline=" true "/> <button android:id=" @+id/btn_add_student "android:layou 
      T_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignbaseline= "@id/et_age" android:layout_torightof= "@id/et_age" android:text= "add Student" android:textsize= "20sp"/> </relativelay Out> &LT;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:paddi ng= "5dip" > </LinearLayout> </ScrollView> <linearlayout android:layout_width= "Fill_par Ent "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= "Save Data" android:textsize= "20SP"/> <button android:id= "@+id/btn_restore" android:layout_width= "Wrap_content" Android:layout_heigh 
      t= "Wrap_content"android:layout_weight= "1" android:text= "restore Data" android:textsize= "20sp"/> </LinearLayout> < 
 /linearlayout>

2, Key code:

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 () &GT 
        0) {if (savestudent2local ()) {Toast.maketext (this, "Save Success", 0). Show (); 
        else {Toast.maketext (this, save failed, 0). Show (); 
      } else {Toast.maketext (this, "No data currently", 0). Show (); 
    } break; 
      Case R.id.btn_restore:if (restorestudentfromlocal ()) {Toast.maketext (this, "restore Success", 0). Show (); 
      else {Toast.maketext (this, restore failed, 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)) {Stude 
          NT = 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 (the 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, "Please enter correctly", 0). Show (); 
  }} 
} 
 

The above is the entire content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.