1

在下面的SSCCE中,想法是主活動中有一個按鈕,當用戶單擊它時(方法showCheckBoxPreferencesValue()被調用)在preferences.xml中定義的唯一首選項的默認值(使用android:default_value)應顯示在此按鈕下方的文本框中。爲什麼我的首選項的defaultValue沒有保存到SharedPreferences?

爲了得到此默認值在文本框中顯示,我使用的方法sharedPreferences.getString()並傳遞Preference does not exist作爲第二個參數,如果偏好(與key作爲第一個參數傳遞)似乎不存在,這意味着,那麼字符串Preference does not exist被設置爲文本框。 (Source

而這種情況發生了嗎?我究竟做錯了什麼?最後看截圖。

RES/XML /的preferences.xml:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" > 

    <EditTextPreference android:key="@string/preferences_editTextPreference_key" 
     android:title="@+id/preferences_editTextPreference_title" 
     android:defaultValue="@string/preferences_editTextPreference_defaultValue" /> 

</PreferenceScreen> 

MainActivity.java:

public class MainActivity extends Activity { 
    private String EDIT_TEXT_PREFERENCE_KEY; 
    private TextView textView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     textView = (TextView) findViewById(R.id.mainActivity_textView); 

     PreferenceManager.setDefaultValues(this, R.xml.preferences, false); 

    } 

    public void showCheckBoxPreferencesValue(View view) { 

     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 

     EDIT_TEXT_PREFERENCE_KEY = getResources().getString(R.string.preferences_editTextPreference_key); 

     String editTextPreferenceValue = sharedPreferences.getString(EDIT_TEXT_PREFERENCE_KEY, 
       "Preference does not exist"); 

     textView.setText("Checkbox Preference Value: " + editTextPreferenceValue); 
    } 
} 

RES /值/ strings.xml中

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">Preferences Practice</string> 
    <string name="mainActivity_button">Show EditText Preference\'s Value</string> 
    <string name="preferences_editTextPreference_key">EditTextPreferenceKey</string> 
    <string name="preferences_editTextPreference_title">EditTextPreference Title</string> 


    <string name="preferences_editTextPreference_defaultValue">EditTextPreference Default Value String</string> 

</resources> 

enter image description here

+1

您的代碼似乎確定。然而,我不確定使用對字符串(在資源文件中)的引用來命名KEY是個很好的解決方案。例如,考慮您有多個語言資源文件的情況:密鑰名稱會改變並打破您的系統。也許你可以解決你的問題unistalling和重新安裝你的應用程序,因爲文檔說「這個方法設置默認值,只有當這種方法從來沒有被稱爲過去」 – GVillani82

回答

2

我試着從你提供的代碼中創建一個新的應用程序。

我不得不重新佈局文件,我所做的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="my.package.test.MainActivity"> 

    <TextView 
     android:id="@+id/mainActivity_textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <Button 
     android:layout_below="@id/mainActivity_textView" 
     android:onClick="showCheckBoxPreferencesValue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="CLICKME" 
     /> 
</RelativeLayout> 

(下一次,請包括它=)! )

我跑的代碼,它看起來像這樣:

enter image description here

因此,大家可以看到,它的工作原理。

我認爲你的問題可能是你不止一次運行你的代碼,沒有正確拼寫第一次(或其他不幸)的值。 您會看到,您選擇發送false作爲setDefaultValues的最後一個參數,這會迫使其僅在默認值(首次啓動該應用程序時)時讀取默認值。嘗試卸載您的應用程序並重新安裝,或將標誌設置爲true。

即:

PreferenceManager.setDefaultValues(this, R.xml.preferences, true); 
+0

非常感謝你的時間。將來我會確保發佈所有代碼。 – Solace

相關問題