我有一个preferences.xml
和Settings.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());
}
}
}