0

just added a Preference-Screen into my app. I want to load and store the given values into my sqlite db. I do not know how to solve that, because I dont know how to access the fields in the oncreate of my method, they have no android:id field but an android:key

Here is my layout

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

  <PreferenceCategory
     android:title="@string/program_options">

     <CheckBoxPreference         
       android:key="pref_opt1"
       android:title="@string/autoLogin"
       android:summary="@string/autoLoginDescription"
       android:defaultValue="true"
       />

  </PreferenceCategory>

  <PreferenceCategory
     android:title="@string/program_informations">

    <ListPreference
        android:key="pref_type"
        android:title="Type"
        android:summary="Select item from array"
        android:entries="@array/types"
        android:entryValues="@array/types_values"
        android:defaultValue="1"
        />

    <EditTextPreference
        android:key="pref_text"
        android:title="Input text"
        android:summary="Tap to enter some text"
        android:dialogTitle="Enter text"
    />

  </PreferenceCategory>

  <Preference
    android:title="Visit us"
    android:summary="SRS-Management GmbH">

    <intent
      android:action="android.intent.action.VIEW"
      android:data="http://paperdynamix.de/" />

  </Preference>

</PreferenceScreen>

and my Activity where i want to access the fields

package de.srs.android.pdixuploader.activies;

import de.srs.android.pdixuploader.R;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.widget.CheckBox;


public class Prefs extends PreferenceActivity {

    private CheckBox autoLogin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //here I want to read and prefill the values of the fields 

        addPreferencesFromResource(R.layout.preferences);
    }                   
}

Thanks

4

1 回答 1

0

首先,不需要“预填充”这些值,因为 Android 会自行处理所有需要的值(第一次使用默认值)。

为了获取值,只需使用以下代码:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
// For example
Boolean pref_opt1 = sp.getBoolean("pref_opt1", true)
// generally
// sp.get[TYPE]("KEY", STANDARD-VALUE);
于 2012-12-06T15:38:57.380 回答