动态添加的EditText preference [英] Dynamically add EditTextPreference

查看:620
本文介绍了动态添加的EditText preference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以动态地添加的EditText preference至preferenceCategory在preferenceFragment?或者设置的EditText preference的号码,然后建立尽可能多的?我会gratefull任何帮助。

Is it possible to dynamically add EditTextPreference to PreferenceCategory in PreferenceFragment? Or set the number of EditTextPreference and then create as many? I will be gratefull for any help

推荐答案

我使用一个辅助类来构建的EditText preferences,使用,我可以这样做:

I am using a helper class to build EditTextPreferences, using that I can do:

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);

您当然也可以只创建没有这个辅助类的EditText上preference,我只要找到code更容易使用时阅读:

You can of course also just create the EditTextPreference without this helper class, I just find the code easier to read when using it:

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;
    }

}

最后用于查找preference表示键的preferenceCategory的示例:

Lastly an example of the PreferenceCategory showing the key used for 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>

编辑:

如果我理解正确的注释中的问题,你可以这样做:

If I understand the questions in the comments correctly, you can do this:

@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);
}

本示例设置综上所述,你当然也可以设置标题,如果这是你想要做什么。

This example sets summary, you can of course also set title if that is what you want to do.

这篇关于动态添加的EditText preference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆