应用生成的XML文件保存在/data/data/应用包名/shared_prefs/name.xml中
(/data/data/com.sinaapp.shared/shared_prefs/ssun.xml)
SharedPreferenceActivity.java源码:
package com.sinaapp.shared; import java.util.Map; import com.sinaapp.service.Service; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.Toast; public class SharedPreferenceActivity extends Activity { /** Called when the activity is first created. */ EditText nameText; EditText ageText; Service service; Map<String, String> map; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); nameText = (EditText)this.findViewById(R.id.nametext); ageText = (EditText)this.findViewById(R.id.agetext); service = new Service(this); map = service.getPreference(); nameText.setText(map.get("name")); ageText.setText(map.get("age")); } public void save(View v){ String name = nameText.getText().toString(); int age = Integer.parseInt(ageText.getText().toString()); service.save(name,age); Toast.makeText(getApplicationContext(), "save success!", Toast.LENGTH_LONG).show(); } }
package com.sinaapp.service; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; public class Service { private Context context; public Service(Context context){ this.context = context; } /** * 保存参数 * @param name * @param age */ public void save(String name, int age){ SharedPreferences preferences = context.getSharedPreferences("ssun", Context.MODE_PRIVATE); Editor edit = preferences.edit(); edit.putString("name", name); edit.putInt("age", age); edit.commit(); } /** * * @return 以前设定的参数 */ public Map<String,String> getPreference(){ SharedPreferences preferences = context.getSharedPreferences("ssun", Context.MODE_PRIVATE); Map<String,String> map = new HashMap<String, String>(); map.put("name", preferences.getString("name", "")); map.put("age", String.valueOf((preferences.getInt("age", 0)))); return map; } }
<?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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/name" /> <EditText android:id="@+id/nametext" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:ems="10" > <requestFocus /> </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/age" /> <EditText android:id="@+id/agetext" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/save" android:onClick="save" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="name">姓名</string> <string name="age">年龄</string> <string name="app_name">用户参数设置</string> <string name="save">保存</string> </resources>