3

設置我有這樣的主要活動:不能獲得偏好與PreferenceActivity

public class Home extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.home); 
     initializeStyle(); 
    } 
    private void initializeStyle() { 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     String foobar = prefs.getString("foobar", "hi"); 
     Log.i("MyActivity", foobar); 
    } 
// ... 
} 

這PreferenceActivity:

public class Preferences extends PreferenceActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 
    } 
} 

有了這個XML:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
<PreferenceCategory 
     android:key="preferences_font" 
     android:title="@string/preferences_font"> 
    <EditTextPreference 
      android:title="@string/preferences_font_size" 
      android:key="foobar" 
      android:defaultValue="12"> 
    </EditTextPreference> 
</PreferenceCategory> 
</PreferenceScreen> 

Log.i打印「嗨」,所以這意味着這種偏好不存在或我命名不好。

任何關於我做錯了的想法?

問候,

+1

默認是hi什麼時候嘗試獲取它。您是否參加了您的偏好活動並保存了一次該值? –

回答

7

這可以幫助你回到正軌,並有工作的偏好:

XML文件:的preferences.xml

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
    <PreferenceCategory android:title="Preferences Font"> 
     <EditTextPreference 
      android:title="Select Font Size" 
      android:key="foobar" 
      android:summary="Enter Font Size" 
      android:defaultValue="12" /> 
    </PreferenceCategory> 
</PreferenceScreen> 

Java文件1:Home.java

public class Home extends Activity { 

    public SharedPreferences prefs; 

    static public String fontPref; //you can make it an int or whatever you need 
    static public String myValue; //to store new pref value 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     setContentView(R.layout.home); 
     initializeStyle(); 
    } 

    private void initializeStyle() { 
     prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     fontPref = prefs.getString("foobar", "hi"); //this doesn't actually change the value 
     myValue = "This is a test"; 
     Log.i("MyActivity", foobar); //won't show change yet 
    } 
} 

JAVA文件2:Prefernce.java

import android.preference.Preference; 
import android.preference.PreferenceActivity; 
import android.preference.PreferenceManager; 
//... and any other imports 

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener{ 

    private SharedPreferences prefs; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 
     //add other stuff here if you need, especially if you might have some 
     //prefs that use buttons and need to set up your onClickListener for one 
    } 

    @Override 
    public void onSharedPreferenceChanged(SharedPreferences sharedPref, String key) { 
     Editor editor = sharedPref.edit(); 
     if(key.equalsIgnoreCase("foobar")){ 
      Home.fontPref = sharedPref.getString("foobar", "some value"); 
      editor.putString("foobar", Home.myValue); //where ever or whatever new value is 
     } 

     editor.commit(); 
     Log.i("PrefsActivity", foobar); //should show change now 
    } 
} 

希望這可以幫助您走上正確的軌道。當然有很多方法可以做這些事情,所以祝你好運希望它有所幫助。

+0

這對我很有用,謝謝。 – BarFoo

相關問題