我有preferences.xml
和Settings.java
...在onCreate
方法中,我想设置preferences
的摘要...有人可以指导我是什么最好的方法吗?
我所知道的是,一种做法是
activityName_text = (EditTextPreference) findPreference("activityName");
activityName_text.setSummary(activityName_text.getText());
但是对于每个键,如果我按照上面的模式,我觉得代码会很糟糕......
的preferences.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>
Settings.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());
}
}
}
答案 0 :(得分: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>
答案 1 :(得分: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);
}