您应该使用Shared Preferences
而不是使用PreferenceActivity
. 在单独的帮助程序类中声明这些引用,而不是将其扩展到 Activity。这使您可以灵活地创建自定义布局。
例子:
public class SharePrefManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "selfhelppref";
//Your configurable fields
public static final String KEY_PREF1 = "pref1";
public static final String KEY_PREF2 = "pref2";
public static final String KEY_PREF3 = "pref3";
public SharePrefManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
//Setter function for configurable field
public void setPref(String key, String value){
editor.putString(key, value);
}
//Getter function for configurable field
public String getPref(String key){
return editor.getString(key);
}
}
参考您的活动
SharePrefManager SM = new SharePrefManager(this);
SM.setPref(SM.KEY_PREF1, "name");
String value = SM.getPref(SM.KEY_PREF1);