1.string.xml
<resources>
<string name="app_name">sharedPreferences</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="save">保存信息</string>
<string name="load">读取信息</string>
</resources>
2.布局文件
activity_main
<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" >
<Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/save"
/>
<Button
android:id="@+id/load"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/load"
/>
</LinearLayout>
show_layout.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" >
<TextView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
/>
</LinearLayout>
3.Activity
SaveData
package com.example.sharedpreferences;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SaveData extends Activity{
private static final String FILE_NAME="sharefile";
private Button save=null;
private Button load=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save=(Button)findViewById(R.id.save);
load=(Button)findViewById(R.id.load);
save.setOnClickListener(new saveListener());
load.setOnClickListener(new loadListener());
}
class saveListener implements OnClickListener{
public void onClick(View v) {
SharedPreferences share=getSharedPreferences(FILE_NAME, MODE_PRIVATE);//指定操作的文件名称
SharedPreferences.Editor editor=share.edit();//编辑文件
editor.putString("name", "zhangsan");
editor.putString("phone", "13635603333");
editor.commit();//提交更新
}
}
class loadListener implements OnClickListener{
public void onClick(View v) {
Intent intent=new Intent(SaveData.this,ShowData.class);
SharedPreferences preferences=getSharedPreferences(FILE_NAME, MODE_PRIVATE);//指定操作的文件名称
String name=preferences.getString("name","");
String phone=preferences.getString("phone","");
intent.putExtra("name",name);
intent.putExtra("phone",phone);
startActivity(intent);
}
}
}
ShowData
package com.example.sharedpreferences;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
public class ShowData extends Activity {
TextView textView=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_layout);
textView=(TextView) findViewById(R.id.show);
Intent intent=getIntent();
String name=intent.getStringExtra("name");
String phone=intent.getStringExtra("phone");
textView.setText(name+" "+phone);
}
}