SharedPreferences

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SharedPreferences相关的知识,希望对你有一定的参考价值。

使用SharedPreferences将姓名和年龄信息保存到文件,然后再读取,如图所示

技术分享

主界面代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="cn.edu.niit.myapplication5.MainActivity"
android:orientation="vertical">

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name" />
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/age"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">

<Button
android:id="@+id/in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/in"
android:textSize="20sp" />

<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/read"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
Strings的代码如下:
<resources>
<string name="app_name">My Application5</string>
<string name="name">姓名</string>
<string name="age">年龄</string>
<string name="in">写入</string>
<string name="read">读取</string>
</resources>
效果图如下:

技术分享

Java部分代码如下:
package cn.edu.niit.myapplication5;




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 evname;
private EditText evage;
//确定按钮
private Button in;
private Button read;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取组件
evname = (EditText) findViewById(R.id.name);
evage = (EditText) findViewById(R.id.age);
in= (Button)findViewById(R.id.in);
//设置按钮监听事件
in.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = evname.getText().toString();
String age =evage.getText().toString();

if(saveToFile(name)){
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 = evname.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 = evname.getText().toString();
String age =evage.getText().toString();
Toast.makeText(MainActivity.this, "输入姓名"+username+",年龄:" + age,Toast.LENGTH_SHORT).show();
}
});
}

private boolean saveToFile(String userName) {
try {
FileOutputStream out = openFileOutput("data.txt", MODE_PRIVATE );
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( out ) );
writer.write(userName);
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

























































































































































以上是关于SharedPreferences的主要内容,如果未能解决你的问题,请参考以下文章

如何在改变sharedpreferences数据后自动改变android menuItem数据?

SharedPreferences 和数据库:在本机代码中还是在 Flutter 中?

当我使用 sharedpreference 获取数据时给出 NullPointerException [重复]

SharedPreferences 的commit和apply分析

使用 sharedpreferences 保存一个 int 变量“highscore”[重复]

SharedPreferences无法在活动中使用