从 JAVA 加载 PreferenceScreen

发布于 2024-09-28 09:27:57 字数 214 浏览 5 评论 0原文

我们如何动态填充 PreferenceScreen(来自 JAVA)。

我有一个字符串数组。我希望所有项目都显示在带有复选框的第二首选项屏幕中。

目前我有

CheckBoxPreference
tags for each item in string array in preferences.xml.

关于如何执行此操作的任何想法。

How can we fill PreferenceScreen dynamically (from JAVA).

I have one string array. I want all items to be displayed in second preference screen with checkbox.

Currently I am having

CheckBoxPreference

tags for each item in string array in preferences.xml.

Any thoughts on how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硪扪都還晓 2024-10-05 09:27:57

以下代码解释了动态实现 PreferenceActivity.Edit 以您的方式。

public class DynamicPreference extends PreferenceActivity{


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        ListView listView = new ListView(this);

        listView.setId(android.R.id.list);
        listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 1));
        layout.addView(listView);

        this.setContentView(layout);

        Preference test1=getPrefereces("key1","Header Preference #1","Summary1",null);

        Preference test2=getPrefereces("key2","Header Preference #2","Summary2",null);


        PreferenceScreen prefScreen=this.getPreferenceManager().createPreferenceScreen(this);

        addPreferenceCategory(prefScreen,"Category",test1,test2);

        //addPreferenceCategory(prefScreen,"",test2);
        this.setPreferenceScreen(prefScreen);
            //add Intent to preferences 
        Preference  prefereces=findPreference("key");
        prefereces.setIntent(new Intent(getApplicationContext(), Nextactivity.class));





    }
    private boolean addPreferenceCategory(PreferenceScreen prefScreen,
            String titleCategory, Preference... preferences) {
        boolean addPreference = false;
        for (Preference preference : preferences) {
            if (preference != null)
                addPreference = true;
        }
        if (addPreference) {

            PreferenceCategory preferenceCategory = new PreferenceCategory(this);

            preferenceCategory.setTitle(titleCategory);

            prefScreen.addPreference(preferenceCategory);
            for (Preference preference : preferences) {
                if (preference != null)
                    preferenceCategory.addPreference(preference);
            }
            return true;
        } else
            return false;
    }
    private Preference getPrefereces(String key,String title, String summary,
            Intent intent) {
        // TODO Auto-generated method stub
        Preference pref=new Preference(this);
        pref.setTitle(title);
        pref.setSummary(summary);

        if(key!=null)
        {
            pref.setKey(key);
        }
        if(intent!=null)
        {
            pref.setIntent(intent);
        }
        return pref;
    }
}

Following code is explained that Implementing the PreferenceActivity Dynamically.Edit in your way.

public class DynamicPreference extends PreferenceActivity{


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        ListView listView = new ListView(this);

        listView.setId(android.R.id.list);
        listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 1));
        layout.addView(listView);

        this.setContentView(layout);

        Preference test1=getPrefereces("key1","Header Preference #1","Summary1",null);

        Preference test2=getPrefereces("key2","Header Preference #2","Summary2",null);


        PreferenceScreen prefScreen=this.getPreferenceManager().createPreferenceScreen(this);

        addPreferenceCategory(prefScreen,"Category",test1,test2);

        //addPreferenceCategory(prefScreen,"",test2);
        this.setPreferenceScreen(prefScreen);
            //add Intent to preferences 
        Preference  prefereces=findPreference("key");
        prefereces.setIntent(new Intent(getApplicationContext(), Nextactivity.class));





    }
    private boolean addPreferenceCategory(PreferenceScreen prefScreen,
            String titleCategory, Preference... preferences) {
        boolean addPreference = false;
        for (Preference preference : preferences) {
            if (preference != null)
                addPreference = true;
        }
        if (addPreference) {

            PreferenceCategory preferenceCategory = new PreferenceCategory(this);

            preferenceCategory.setTitle(titleCategory);

            prefScreen.addPreference(preferenceCategory);
            for (Preference preference : preferences) {
                if (preference != null)
                    preferenceCategory.addPreference(preference);
            }
            return true;
        } else
            return false;
    }
    private Preference getPrefereces(String key,String title, String summary,
            Intent intent) {
        // TODO Auto-generated method stub
        Preference pref=new Preference(this);
        pref.setTitle(title);
        pref.setSummary(summary);

        if(key!=null)
        {
            pref.setKey(key);
        }
        if(intent!=null)
        {
            pref.setIntent(intent);
        }
        return pref;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文