在main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/nameinfo"
android:gravity="center_horizontal"
android:layout_marginTop="8dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/ageinfo"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
在MySharedPreferencesDemo.java程序中
package com.li.sharedpreferences;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
public class MySharedPreferencesDemo extends Activity {
private static final String FILENAME = "BaoCunDeWenJianMingCheng"; //保存的文件名称
private TextView nameinfo = null;
private TextView ageinfo = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.nameinfo = (TextView)super.findViewById(R.id.nameinfo);
this.ageinfo = (TextView)super.findViewById(R.id.ageinfo);
SharedPreferences share = super.getSharedPreferences(FILENAME
, Activity.MODE_PRIVATE);
this.nameinfo.setText("姓名:" + share.getString("name", "没有作者信息!"));
this.ageinfo.setText("年龄:" + share.getInt("age", 0));
}
}