0
我目前正在使用带片段的偏好活动。我在定义这些首选项的xml文件中使用android:summary来为用户设置说明标题,然后在用户更改首选项值并且一切正常时,在每个首选项上设置汇总。 但是,当用户在任何EditTextPreference中清除他/她的选择时,我想从xml标记android:summary中显示回解释文本。我怎么能做到这一点?android偏好返回到默认摘要
在此先感谢您的帮助。
我目前正在使用带片段的偏好活动。我在定义这些首选项的xml文件中使用android:summary来为用户设置说明标题,然后在用户更改首选项值并且一切正常时,在每个首选项上设置汇总。 但是,当用户在任何EditTextPreference中清除他/她的选择时,我想从xml标记android:summary中显示回解释文本。我怎么能做到这一点?android偏好返回到默认摘要
在此先感谢您的帮助。
好的我想通了。 只需为每个首选项密钥添加一个名为“key_summary”的资源字符串,并将其用作定义首选项的xml文件中的android:summary。 然后在onSharedPreferenceChanged功能,只需加载相应的字符串作为内容摘要如下:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
Preference preference = findPreference(key);
if(preference instanceof EditTextPreference)
{
EditTextPreference editPref = (EditTextPreference)preference;
String szPref = editPref.getText();
if(null!=szPref&&0<szPref.length())
editPref.setSummary(szPref);
else
{
Activity activity = getActivity();
int nSummary = getResources().getIdentifier(key + "_summary", "string", activity.getPackageName());
if(0<nSummary)
editPref.setSummary(getString(nSummary));
else
editPref.setSummary(szPref);
}
}
}
您应该设置默认summray在strings.xml中字符串资源和您可以设置回每次你想要的。 – Opiatefuchs
是的,但我的类是每个EditTextPreference使用的通用类,我有很多。我不打算在该类中添加特定的条目以获取每个条目的相应字符串。我想要一个更通用的方式来做到这一点... 类似setSummary(getDefaultSummary())... :) – user3747760