【实战】学生信息保存到xm文件l中
来源:互联网 发布:周立功单片机应用技巧 编辑:程序博客网 时间:2024/05/29 16:56
通过这个可以将学生会的姓名、学号的性别保存到单独的学生的xml文件中
一、直接写入到学生的xml文件中
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textSize="30sp" /> <EditText android:id="@+id/student_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入学生的姓名" /> <EditText android:id="@+id/student_number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入学生的学号" /> <RadioGroup android:id="@+id/radiogb" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/male" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:checked="true" android:text="男" /> <RadioButton android:id="@+id/female" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:text="女" /> </RadioGroup><Button android:id="@+id/save" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="保存"/></LinearLayout>
MainActivity.xml:
package com.lcz.student;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.OutputStream;import android.os.Bundle;import android.app.Activity;import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;import android.text.TextUtils;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends Activity implements View.OnClickListener {private Button save_btn;private EditText ed_ssnumber;private EditText ed_ssname;private RadioGroup rgb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); save_btn=(Button) findViewById(R.id.save); ed_ssname=(EditText) findViewById(R.id.student_name); ed_ssnumber=(EditText) findViewById(R.id.student_number); rgb=(RadioGroup) findViewById(R.id.radiogb); save_btn.setOnClickListener(this); } @Overridepublic void onClick(View v) {// TODO Auto-generated method stubString studentname = ed_ssname.getText().toString().trim();String studentnumber = ed_ssnumber.getText().toString().trim();if(TextUtils.isEmpty(studentname)||TextUtils.isEmpty(studentnumber)){Toast.makeText(this, "学生的姓名和学号不能为空", 0).show();return;}//获得学生的性别int id = rgb.getCheckedRadioButtonId();String sex="男";if(id==R.id.male){sex="男";}else{sex="女";}//到这一步,学生的信息就都有了try {File file=new File(getFilesDir(),studentname+".xml");StringBuilder sb=new StringBuilder();sb.append("<?xml version='1.0' encoding=='utf-8'?>");sb.append("<student>");sb.append("<name>");sb.append(studentname);sb.append("</name>");sb.append("<number>");sb.append(studentnumber);sb.append("</number>");sb.append("<sex>");sb.append(sex);sb.append("</sex>");sb.append("</student>");OutputStream out=new FileOutputStream(file);out.write(sb.toString().getBytes());out.close();Toast.makeText(this, "保存"+studentname+"成功...", 0).show();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();Toast.makeText(this, "保存"+studentname+"失败...", 0).show();}} }
MainActivity.java:
package com.lcz.student;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.OutputStream;import org.xmlpull.v1.XmlSerializer;import android.os.Bundle;import android.app.Activity;import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;import android.text.TextUtils;import android.util.Xml;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends Activity implements View.OnClickListener {private Button save_btn;private EditText ed_ssnumber;private EditText ed_ssname;private RadioGroup rgb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); save_btn=(Button) findViewById(R.id.save); ed_ssname=(EditText) findViewById(R.id.student_name); ed_ssnumber=(EditText) findViewById(R.id.student_number); rgb=(RadioGroup) findViewById(R.id.radiogb); save_btn.setOnClickListener(this); } @Overridepublic void onClick(View v) {// TODO Auto-generated method stubString studentname = ed_ssname.getText().toString().trim();String studentnumber = ed_ssnumber.getText().toString().trim();if(TextUtils.isEmpty(studentname)||TextUtils.isEmpty(studentnumber)){Toast.makeText(this, "学生的姓名和学号不能为空", 0).show();return;}//获得学生的性别int id = rgb.getCheckedRadioButtonId();String sex="男";if(id==R.id.male){sex="男";}else{sex="女";}//到这一步,学生的信息就都有了try {File file=new File(getFilesDir(),studentname+".xml");OutputStream out=new FileOutputStream(file);//专门生成xml文件的序列化器XmlSerializer serializer = Xml.newSerializer();serializer.setOutput(out, "UTF-8");serializer.startDocument("UTF-8", true);serializer.startTag(null, "student");serializer.startTag(null, "name");//设置文本信息serializer.text(studentname);serializer.endTag(null, "name");serializer.startTag(null, "number");//设置文本信息serializer.text(studentnumber);serializer.endTag(null, "number");serializer.startTag(null, "sex");//设置文本信息serializer.text(sex);serializer.endTag(null, "sex");serializer.endTag(null, "student");serializer.endDocument();out.close();Toast.makeText(this, "保存"+studentname+"成功...", 0).show();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();Toast.makeText(this, "保存"+studentname+"失败...", 0).show();}} }
阅读全文
0 0
- 【实战】学生信息保存到xm文件l中
- 用c语言输入学生信息保存到文件中并读取文件内容
- 将学生信息保存到文件中,并读出文件内容
- 用文件保存学生信息
- 保存学生信息到文件(数组内元素排序, io操作)
- 学生信息记录文件中
- dmesg打印信息如何后台保存到文件中
- dmesg打印信息如何后台保存到文件中
- [C/C++]随机生成信息,并保存到文件中
- dmesg打印信息如何后台保存到文件中
- Qt-将QDebug信息保存到文件中
- 练习保存学生信息
- 保存学生信息
- 异质链表保存学生信息
- 文件部分-----(6 )二进制文件读写操作----将学生信息写入到文件中,再从文件中读取信息
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 实现学生信息的文件保存和读取
- 第十六周项目1用文件保存学生信息
- Win10+Anaconda+python3.6安装Tensorflow
- 文件下载
- B&G GLOBAL MARKETS全球影响力如何
- 融云通讯简易集成
- Java泛型中E、T、K、V等的含义
- 【实战】学生信息保存到xm文件l中
- Flume 概述架构及部署
- [python]报错汇总【持续更新】
- 三点估算
- Linux 服务器安装MySQL数据库
- Android 冷门问题记录(11.30)
- C# 如何更改程序集名称
- com.alibaba.fastjson.JSONObject cannot be cast to com.panodata.model.riskUtil.ModelPOBean
- Gradle 添加多项,依赖其他项目