【实战】学生信息保存到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();}}    }


二、通过谷歌提供的api接口来完成
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
原创粉丝点击