首页 > 代码库 > SharedPreferences
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。