1

我有一个preferences.xmlSettings.java...在onCreate我想设置摘要的方法中preferences...有人可以指导我什么是最好的方法吗???我所知道的是,一种做法是

activityName_text = (EditTextPreference) findPreference("activityName");
    activityName_text.setSummary(activityName_text.getText());

但是对于每个键,如果我遵循上述模式,我觉得代码会很糟糕......

首选项.xml

    <PreferenceScreen android:title="Header" >
        <EditTextPreference
            android:defaultValue="TestRequest"
            android:key="activityName"
            android:title="activityName" />
        <EditTextPreference
            android:defaultValue="Request"
            android:key="msgName"
            android:title="msgName" />

        <ListPreference
            android:defaultValue="0"
            android:entries="@array/msg_type"
            android:entryValues="@array/msg_type_values"
            android:key="msgType"
            android:title="msgType" />

        <EditTextPreference
            android:defaultValue="AA"
            android:key="senderURI"
            android:title="senderURI" />
        <EditTextPreference
            android:defaultValue="BB"
            android:key="destinationURI"
            android:title="destinationURI" />
        <EditTextPreference
            android:defaultValue="AA"
            android:key="replyToURI"
            android:title="replyToURI" />
        <EditTextPreference
            android:defaultValue="BB"
            android:key="originatorURI"
            android:title="originatorURI" />
        <EditTextPreference
            android:defaultValue="FF"
            android:key="failureReplytoURI"
            android:title="failureReplytoURI" />
        <EditTextPreference
            android:defaultValue="AA"
            android:key="correlationId"
            android:title="correlationId" />
        <EditTextPreference
            android:defaultValue="HH"
            android:key="security"
            android:title="security" />
        <EditTextPreference
            android:defaultValue="GG"
            android:key="securityType"
            android:title="securityType" />
        <EditTextPreference
            android:defaultValue="HH"
            android:key="priority"
            android:title="priority" />

        <ListPreference
            android:defaultValue="0"
            android:entries="@array/communicationPattern"
            android:entryValues="@array/communicationPatternValues"
            android:key="communicationPattern"
            android:title="communicationPattern" />
        <ListPreference
            android:defaultValue="0"
            android:entries="@array/communicationStyle"
            android:entryValues="@array/communicationStyleValues"
            android:key="communicationStyle"
            android:title="communicationStyle" />


        <EditTextPreference
            android:defaultValue="100"
            android:digits="0123456789"
            android:key="requestedBatchSize"
            android:title="requestedBatchSize" />
        <!-- radhika-number format -->
        <EditTextPreference
            android:defaultValue="78"
            android:digits="0123456789"
            android:key="batchSequenceNumber"
            android:title="batchSequenceNumber" />

        <ListPreference
            android:defaultValue="0"
            android:entries="@array/batchSequenceEndOfReply"
            android:entryValues="@array/batchSequenceEndOfReplyValues"
            android:key="batchSequenceEndOfReply"
            android:title="batchSequenceEndOfReply" />

        <EditTextPreference
            android:defaultValue="HH"
            android:key="iteratorReferenceURI"
            android:title="iteratorReferenceURI" />
        <EditTextPreference
            android:defaultValue="KK"
            android:key="fileLocationURI"
            android:title="fileLocationURI" />
        <EditTextPreference
            android:defaultValue="10/10/2010"
            android:key="timestamp"
            android:title="timestamp" />
        <EditTextPreference
            android:defaultValue="kkk"
            android:key="vendorExtensions"
            android:title="vendorExtensions" />

        <PreferenceCategory
            android:key="activityStatus"
            android:title="activityStatus" >
            <ListPreference
                android:defaultValue="0"
                android:entries="@array/activityStatusType"
                android:entryValues="@array/activityStatusTypeValues"
                android:key="actStatus"
                android:title="activityStatus" />

            <EditTextPreference
                android:defaultValue="AA"
                android:key="qualifier"
                android:title="qualifier" />
        </PreferenceCategory>


        <PreferenceCategory
            android:key="msgSpecificProperties"
            android:title="msgSpecificProperties" >
            <EditTextPreference
                android:defaultValue="multiple values"
                android:key="propName"
                android:title="propName" />
            <EditTextPreference
                android:defaultValue="multiple values"
                android:key="propValue"
                android:title="propValue" />
        </PreferenceCategory>
        <PreferenceCategory
            android:key="compressionType"
            android:title="compressionType" >
            <EditTextPreference
                android:defaultValue="kkk"
                android:key="compressionTypeAttr"
                android:title="extension" />

            <ListPreference
                android:defaultValue="0"
                android:entries="@array/compressionType"
                android:entryValues="@array/compressionTypeValues"
                android:key="compressionTypeValue"
                android:title="value" />
        </PreferenceCategory>
        <PreferenceCategory
            android:key="packingType"
            android:title="packingType" >
            <EditTextPreference
                android:defaultValue="kkk"
                android:key="packingTypeAttr"
                android:title="extension" />

            <ListPreference
                android:defaultValue="0"
                android:entries="@array/packingType"
                android:entryValues="@array/packingTypeValues"
                android:key="packingTypeValue"
                android:title="value" />
        </PreferenceCategory>
    </PreferenceScreen>
    <PreferenceScreen android:title="Body" >
        <PreferenceScreen android:title="Managed Elements" >
            <PreferenceCategory
                android:key="mdOrMlsnRef"
                android:title="md Or Mlsn Ref" >
                <EditTextPreference
                    android:defaultValue="Name"
                    android:key="rdnName"
                    android:title="rdnName" />
                <EditTextPreference
                    android:defaultValue="Value"
                    android:key="rdnValue"
                    android:title="rdnValue" />
            </PreferenceCategory>
        </PreferenceScreen>
        <PreferenceScreen android:title="Active Alarms" >
        </PreferenceScreen>
    </PreferenceScreen>
</PreferenceScreen>

设置.java:

package com.example.settingstest;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;


public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    EditTextPreference activityName_text;
    EditTextPreference msgName_text;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        addPreferencesFromResource(R.xml.preferences);  
    }

    @Override
    protected void onResume() {
        super.onResume();       
        getPreferenceScreen().getSharedPreferences()
        .registerOnSharedPreferenceChangeListener(this);
    }

    @Override protected void onPause() {
        super.onPause();        
        getPreferenceScreen().getSharedPreferences()
        .unregisterOnSharedPreferenceChangeListener(this); 
    }   


    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        Preference p = findPreference(key);
        if(p instanceof EditTextPreference)
        {           
            p.setSummary(sharedPreferences.getString(key, (String)p.getSummary()));
        }
        else if(p instanceof ListPreference)
        {           
            p.setSummary((String)((ListPreference) p).getEntry());
        }       
    }
}
4

2 回答 2

0

像这样使用它总是更好地从 prefrence.xml 设置摘要,我举一个例子,我正在使用这段代码并且它工作得很好

<PreferenceCategory
        android:key="dbh_social"
        android:title="We know you are social" >
    <Preference
            android:key="dbh_invite"
            android:summary="Invite your friends"
            android:title="Invite Facebook Friends" />
    <Preference
            android:key="dbh_wall_post"
            android:summary="Post about us on your Facebook wall "
            android:title="Post on Facebook Wall" />
    </PreferenceCategory>
于 2013-04-08T12:09:16.383 回答
0

您可以将所有键放入字符串数组资源并在 onCreate() 上对其进行迭代

final String[] keys = getResources().getStringArray(R.array.keys) ; // all your keys here
SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

for (int i = 0; i < keys.length; i++) {
    setPreferenceSummary(p, keys[i]);
}


private void setPreferenceSummary(SharedPreferences sharedPreferences, String key) {
    final Preference p = findPreference(key);
    if(p instanceof EditTextPreference)
    {           
        p.setSummary(sharedPreferences.getString(key, (String)p.getSummary()));
    }
    else if(p instanceof ListPreference)
    {           
        p.setSummary((String)((ListPreference) p).getEntry());
    }
}


public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    setPreferenceSummary(sharedPreferences, key);
}
于 2013-04-08T12:31:28.030 回答