例如要保存用户的姓名和年龄两个参数,如下图布局

wKioL1M1k5CwbxUGAABK7-3R2xU671.jpg

1.在strings.xml文件中声明要使用到的字符串


wKiom1M1lGPRviveAAGVmiCK-UU525.jpg

2.在布局文件中添加<TextView/>,<EditText/>,<Button />控件,实现上图的布局

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/name" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/name" />
               
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/age" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numeric="integer"
        android:id="@+id/age" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:onClick="save"
        />

3.MainActivity的代码如下

public class MainActivity extends Activity {
   private EditText  nameText;
   private EditText  ageText;
   private PreferencesService service;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        nameText = (EditText)this.findViewById(R.id.name);
        ageText = (EditText)this.findViewById(R.id.age);
        service = new PreferencesService(this);//每次点击保存都会实例化该方法
    }
                                                                                                               
    public void save(View v) {
        String name = nameText.getText().toString();
        String age = ageText.getText().toString();
        //PreferencesService service = new PreferencesService(this);//每次点击保存都会实例化,放到启动方法中
        service.save(name,Integer.valueOf(age));
        Toast.makeText(getApplicationContext(), R.string.success, 1).show();
    }
}

4.PreferencesService类的代码如下


public class PreferencesService {
    private Context context;
    public PreferencesService(Context context) {//得到上下对象
        this.context = context;
    }
    /**
     * 保存参数
     * @param name
     * @param age
     */
    public void save(String name, Integer age) {
        //取得SharePreferences对象,通过上下文环境得到
        SharedPreferences preferences = context.getSharedPreferences("gao", Context.MODE_PRIVATE);
         Editor editor = preferences.edit();//得到编辑器对象
         editor.putString("name", name);
         editor.putInt("age", age);//到此数据保存在内存中
         editor.commit();//把内存中的数据提交到文件中
    }
}

运行结果产生的xml文件中的数据


wKioL1M1lVDyyx3wAADAQ0vaTBM544.jpg

   以上即实现了用户自己对软件偏好参数的保存,那么如何读取用户的偏好参数呢?如用户打开上述软件时,显示用户的参数设置,如下图

wKioL1M2IanBMtRgAAFZaLtjTKY845.jpg

   实现方法是,在PreferencesService类中添加getPreferences()方法,具体代码如下

/**
     * 获取各项配置参数
     * @return  参数值
     */
    public Map<String,String> getPreferences(){
        Map<String,String> params = new HashMap<String,String>();
        //取得SharePreferences对象,通过上下文环境得到,"gao"是之前保存好的数据名称,注意不带后缀名
        SharedPreferences preferences = context.getSharedPreferences("gao", Context.MODE_PRIVATE);
        params.put("name", preferences.getString("name", "you name"));
        params.put("age", String.valueOf(preferences.getInt("age", 0)));
        return params;
    }

   在MainActivity类的OnCreate()方法中添加如下代码

//第一次运行时显示参数
        Map<String,String> params = service.getPreferences();
        nameText.setText(params.get("name"));
        ageText.setText(params.get("age"));

当再次执行时,用户输入新的数据并点击保存,那么就会保存最近的用户输入的数据。

注:附件文章中的代码。