使用sharedpreferences将姓名和年龄信息保存到文件中
**本次作业和刚开始要求做的登陆界面大同小异,比较容易实现
**XML布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.mpyypm.sharedpreferences.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/ev_userName"
android:id="@+id/ev_userName"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/ev_password"
android:id="@+id/ev_age"/>
</LinearLayout>
-<LinearLayout
android:orientation="horizontal"
android:layout_height="0dp"
android:layout_width="wrap_content"
android:layout_weight="1">
<Button
android:layout_height="wrap_content"
android:layout_width="187dp"
android:id="@+id/writer"
android:text="@string/writer"
android:onClick="onClick"/>
<Button
android:layout_height="wrap_content"
android:layout_width="204dp"
android:id="@+id/read"
android:text="@string/read"
android:onClick="onClick"/>
</LinearLayout>
</LinearLayout>
**JAVA代码的实现
package com.example.mpyypm.sharedpreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity {
//定义相关组件
private EditText evuserName;
private EditText evage;
//确定按钮
private Button writer;
private Button read;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取这些组件findViewId(让其发生关联)
evuserName = (EditText) findViewById(R.id.ev_userName);
evage = (EditText) findViewById(R.id.ev_age);
writer= (Button)findViewById(R.id.writer);
//设置按钮(写入)监听事件
writer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userName = evuserName.getText().toString();
String age = evage.getText().toString();
//根据情况进行存储,这里需要在OnClick方法外使用一个saveToFile方法,对其进行写入
if(saveToFile(userName)){
Toast.makeText( MainActivity.this,"保存成功" ,Toast.LENGTH_SHORT).show();
}
}
});
//设置按钮(读取)监听事件
read = (Button)findViewById(R.id.read);
read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userName = evuserName.getText().toString();
String filename = "data.txt";
String result = "";
try{
//打开指定文件,并读取其数据
FileInputStream in = openFileInput( filename );
BufferedReader reader = new BufferedReader( new InputStreamReader( in ) );
userName = reader.readLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
userName = evuserName.getText().toString();
String age = evage.getText().toString();
Toast.makeText(MainActivity.this, "输入姓名"+userName+",年龄:" + age,Toast.LENGTH_SHORT).show();
}
});
}
private boolean saveToFile(String userName) {
//1.打开文件
try {
FileOutputStream out = openFileOutput("data.txt", MODE_PRIVATE );
//2.写入
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( out ) );
writer.write(userName);
//3.关闭文件输入流
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}