是否可以在 PreferenceFragment 中将 EditTextPreference 动态添加到 PreferenceCategory?或者设置EditTextPreference的数量然后创建多少?我将不胜感激任何帮助
问问题
773 次
1 回答
2
我正在使用一个帮助类来构建 EditTextPreferences,我可以使用它:
Context context = getActivity();
PreferenceCategory statusCat = (PreferenceCategory) findPreference(context
.getString(R.string.status_category));
EditTextPreference editTextPref = EditTextPrefBuilder.getBuilder(context)
.setTitle("title")
.setSummary("summary")
.build();
statusCat.addPreference(editTextPref);
当然,您也可以在没有此帮助程序类的情况下创建 EditTextPreference,我只是发现使用它时代码更易于阅读:
public class EditTextPrefBuilder {
private final Context context;
private String title;
private String summary;
private boolean enabled;
public static EditTextPrefBuilder getBuilder(Context context) {
return new EditTextPrefBuilder(context);
}
EditTextPreference ep = null;
public EditTextPreference build() {
ep = new EditTextPreference(context);
ep.setTitle(title);
ep.setSummary(summary);
ep.setEnabled(enabled);
return ep;
}
private EditTextPrefBuilder(Context context) {
super();
this.context = context;
this.title = "";
this.summary = "";
this.enabled = false;
}
public EditTextPrefBuilder setTitle(String title) {
this.title = title;
return this;
}
public EditTextPrefBuilder setSummary(String summary) {
this.summary = summary;
return this;
}
public EditTextPrefBuilder setEnabled(boolean enabled) {
this.enabled = enabled;
return this;
}
}
最后一个 PreferenceCategory 示例显示了用于 findPreference 的键:
<PreferenceCategory
android:key="@string/status_category"
android:shouldDisableView="false"
android:title="@string/module_status" >
<SwitchPreference
android:defaultValue="true"
android:enabled="false"
android:key="@string/key_module_activated_basis"
android:switchTextOff="@string/deactive"
android:switchTextOn="@string/active" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="@string/key_module_activated_basis"
android:enabled="false"
android:key="@string/status_checkbox"
android:selectable="false"
android:summary="@string/please_wait"
android:title="@string/checking" />
</PreferenceCategory>
编辑:
如果我正确理解评论中的问题,您可以这样做:
@Override public void onStart() {
// update initial values
updateSummaries();
super.onStart();
}
@Override
public void onResume() {
// listen for when preference values change
PreferenceManager.getDefaultSharedPreferences(context)
.registerOnSharedPreferenceChangeListener(this);
super.onResume();
}
@Override
public void onPause() {
PreferenceManager.getDefaultSharedPreferences(context)
.unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
@Override public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// for simplicity update all summaries, could also inspect the key to only update that single Preference
updateSummaries();
}
private void updateSummaries() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// update the summary of the EditText
String editSummary = prefs.getString("key_for_edittext");
(EditTextPreference)findPreference("key_for_edittext").setSummary(editSummary);
}
此示例设置摘要,如果您想要这样做,您当然也可以设置标题。
于 2014-05-13T15:36:24.997 回答