Android实现学生管理系统

 更新时间:2016年03月17日 16:57:16   作者:吴英强  
这篇文章主要为大家详细介绍了Android实现学生管理系统的相关代码,供大家学习借鉴,感兴趣的小伙伴们可以参考一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

本文实例为大家分享了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串口通讯SerialPort的使用详情

    Android串口通讯SerialPort的使用详情

    这篇文章主要介绍了Android串口通讯SerialPort的使用详情,文章围绕主题展开详细的内容戒杀,具有一定的参考价值,需要的朋友可以参考一下
    2022-09-09
  • Android中实现下载URL地址的网络资源的实例分享

    Android中实现下载URL地址的网络资源的实例分享

    这篇文章主要介绍了Android中实现下载URL地址的网络资源的实例,其中还有一个进行多线程下载的Java代码示例,非常典型,需要的朋友可以参考下
    2016-04-04
  • 谷歌被屏蔽后如何搭建安卓环境

    谷歌被屏蔽后如何搭建安卓环境

    从5月27日开始,谷歌(Google)在华的几乎所有的服务都处于无法使用的状态,除了搜索引擎遭到屏蔽之外,谷歌的邮箱(Gmail)、日历(Calendar)、翻译(Translate)、地图(Maps)、分析(Analytics)和Google AdSense等产品也受到了影响。同时安装安卓环境的时候同样容易出现问题
    2014-06-06
  • 详细分析Fresco源码之图片加载流程

    详细分析Fresco源码之图片加载流程

    Fresco是一个强大的图片加载组件,使用它之后,你不需要再去关心图片的加载和显示这些繁琐的事情!它支持Android2.3及以后的版本
    2021-01-01
  • Android实现自定义华丽的水波纹效果

    Android实现自定义华丽的水波纹效果

    关于Android的水波纹效果小编之前给大家也分享几篇类似的,有兴趣可通过下面的相关文章进行查看,今天给大家再分享一个华丽的水波纹效果,这个效果很不错,感兴趣的可以参考借鉴。
    2016-08-08
  • 基于Android实现数独游戏

    基于Android实现数独游戏

    这篇文章主要为大家详细介绍了基于Android实现数独游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • 详谈android 6.0 fuse文件系统的挂载和卸载问题

    详谈android 6.0 fuse文件系统的挂载和卸载问题

    今天小编就为大家分享一篇详谈android 6.0 fuse文件系统的挂载和卸载问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Android自定义控件实现颜色选择器

    Android自定义控件实现颜色选择器

    这篇文章主要为大家详细介绍了Android自定义控件实现颜色选择器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • Android 实现电话来去自动录音的功能

    Android 实现电话来去自动录音的功能

    本文主要介绍Android 电话自动录音功能的开发,这里提供实现代码和实现效果图,有需要的小伙伴可以参考下
    2016-08-08
  • Android Activity启动模式之singleTop实例详解

    Android Activity启动模式之singleTop实例详解

    这篇文章主要介绍了Android Activity启动模式之singleTop,结合实例形式较为详细的分析了singleTop模式的功能、使用方法与相关注意事项,需要的朋友可以参考下
    2016-01-01

最新评论

?


http://www.vxiaotou.com