在加入Android的动态复选框preferences,并在preference屏幕上显示出来? [英] Adding dynamic checkbox preferences in Android, and display them in preference screen?

查看:240
本文介绍了在加入Android的动态复选框preferences,并在preference屏幕上显示出来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的功能在这里用户可以选择使用复选框共享preferences显示哪些项目组。要precise我会读从preferences和检查的项目展示。

下面是我的preferences类

 公共类preferences扩展$ P $ {pferenceActivity   @覆盖
   保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);             //一个办法添加默认preferences
    //add$p$pferencesFromResource(R.xml.$p$pfs);             //现在我preFER这
    集preferenceScreen(默认值preF());}    //第一次启动应用程序时这应该是读
私人preferenceScreen默认preF(){
    。preferenceScreen根= GET preferenceManager()创建preferenceScreen(本);    复选框preference复选框preF =新的复选框preference(本);
    复选框pref.setKey(1);
    复选框pref.setTitle(SomeRandomStuff);
    root.add preference(复选框preF);    返回根;
 }
     公共SHOWALL preferences(){
          //显示所有的preFERENCES但不知道如何显示它们
     }
}

现在我不明白我怎么动态添加更多的preferences和preference屏幕上显示出来。

下面是主要的活动课

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    尝试{
        的setContentView(R.layout.main);
    }赶上(例外五){
        e.printStackTrace();
    }    超高感度=(ExpandableListView)findViewById(R.id.expandableListView1);    //东西加入preFERENCES
    EDITTEXT =(EditText上)findViewById(R.id.editText1);
    //按钮添加preFERENCES。(搜索字词识别并加入到preF)的
    Add按钮=(的ImageButton)findViewById(R.id.imageButton1);
    //按钮显示preFERENCES
    prefButton =(的ImageButton)findViewById(R.id.imageButton preF);    共享preferences设置= preferenceManager.getDefaultShared preferences(本);
    最后一个共享preferences.Editor编辑= settings.edit();    addButton.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根            prefObject OBJ =新的prefObject();
            字符串键= Integer.toString(I)
            。字符串title = editText.getText()的toString();
            //$p$pfArray.add(obj);
            editor.putString(键,标题);
            editor.commit();
                            我++        }
    });
    prefButton.setOnClickListener(新OnClickListener(){
        //这个方法应该显示用新数据preferences活动
        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            意向意图=新意图(Main.this,preferences.class);
                    startActivity(意向);
                            //我知道如何调用的意图,但我不知道
                            //如何读取保存的内容,并显示它
                            preferences preF =新的preferences();
                            pref.showAll preferences();        }
    });


解决方案

据我所知,有一个preferences没有可见性选项,它还挺有道理的,当你认为这一切都只是一个ListView。

请参阅[ 1 。对于我所看到的,你可以做这样的事情:

  preferenceScreen屏幕= this.get preferenceScreen();
//使用1,因为你用1来创建它。
复选框preference ckbox =(复选框preference)this.find preference(1);
screen.remove preference(ckbox);

要重新创建,你可以这样做[ 2 ]:

  screen.add preference(ckbox);

此外,记得要创建一个使用 setOrder(INT顺序)你的preference ,这样当你重新创建,它将在适当的位置重新创建。

正如你所看到的,它可能是值得保持一个全球参考preference,使其更容易和更快。

当然,我并不需要告诉你应该是逻辑集成到您的复选框preference监听器。请参见此答案由其他人比雷托•梅耶尔本人看到这样做的一个很好的方式(这是一个复选框,太)。在那里,他注册了一个监听器到整个屏幕,并检查其中preference引发的监听器,但你可以做更简单(但更详细的以后)只需设置其西顿preferenceChangeListener

*编辑:我看到你还使用一个按钮来添加preference。您也可以实现同样的逻辑上面到按钮本身。这一切都取决于如果你想做到这一点使用复选框或按钮。

最后,只需设置启用状态,除非你正在做的事情,如见先进preferences什么值得从做危险的东西给你的应用程序保持新手用户远离它可能是值得的。但一般使各国对用户体验更好地工作,恕我直言。

我希望这回答了你的问题。

I want to implement functionality where user will be able to select which group of items to be displayed using checkbox shared preferences. To be precise I will read checked items from the preferences and display.

Here is my preferences class

 public class Preferences extends PreferenceActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

             //One way to add default preferences
    //addPreferencesFromResource(R.xml.prefs);

             //For now I prefer this
    setPreferenceScreen(defaultPref());

}

    // The first time application is launched this should be read
private PreferenceScreen defaultPref() {
    PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);

    CheckBoxPreference checkboxPref = new CheckBoxPreference(this);
    checkboxPref.setKey("1");
    checkboxPref.setTitle("SomeRandomStuff");
    root.addPreference(checkboxPref);

    return root;
 }
     public showAllPreferences () {
          // TO SHOW ALL THE PREFERENCES BUT NOT SURE HOW TO DISPLAY THEM
     }
}

Now I cannot understand how do I add more preferences dynamically and display them in preference screen.

Here is the main activity class

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        setContentView(R.layout.main);
    }catch (Exception e) {
        e.printStackTrace();
    }

    exView = (ExpandableListView) findViewById(R.id.expandableListView1);

    // STUFF TO ADD IN PREFERENCES 
    editText = (EditText) findViewById(R.id.editText1);
    //BUTTON TO ADD PREFERENCES.(SEARCH TERM IS IDENTIFIED AND ADDED TO PREF)
    addButton = (ImageButton) findViewById(R.id.imageButton1);
    // BUTTON TO DISPLAY PREFERENCES
    prefButton = (ImageButton) findViewById(R.id.imageButtonPref);

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    final SharedPreferences.Editor editor = settings.edit();

    addButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            PrefObject obj = new PrefObject();
            String key = Integer.toString(i);
            String title = editText.getText().toString();
            //prefArray.add(obj);
            editor.putString(key, title);
            editor.commit();
                            i++

        }
    });
    prefButton.setOnClickListener(new OnClickListener() {
        // This method should show the preferences activity with new data
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Main.this, Preferences.class);
                    startActivity(intent);
                            // I know how to call the intent but I am not sure if
                            // how to read the saved contents and display it
                            Preferences pref = new Preferences();
                            pref.showAllPreferences();  





        }
    });

解决方案

AFAIK, there is no "visibility" option for preferences, which kinda makes sense when you think it's all just a ListView.

See [1]. For what I see, you could do something like this:

PreferenceScreen screen = this.getPreferenceScreen();
// Use "1" since you're using "1" to create it.
CheckBoxPreference ckbox = (CheckBoxPreference) this.findPreference("1");
screen.removePreference(ckbox);

To recreate, you could do this [2]:

screen.addPreference(ckbox);

Additionally, remember to create your preference using the setOrder(int order) so that when you recreate, it will be recreated in the proper position.

As you can see, it could be worth to keep a global reference to the preference to make it easier and faster.

Of course, I don't need to tell that you should integrate that logic into your CheckboxPreference listener. See this answer by nobody else than Reto Meier himself to see a good way of doing it (it's a checkbox, too). There he registers a listener to the whole screen and checks which preference triggered the listener, but you can do it simpler (but more verbose later on) by just setting its setOnPreferenceChangeListener.

*edit: I see that you're also using a button to add the preference. You can also implement the same logic above into the button itself. It all depends if you want to do this using a checkbox or a button.

Finally, it could be worth to just set the enabled state, unless you are doing something like "see advanced preferences" or something worth to keep novice users away from doing dangerous stuff to your app. But generally the enable states work better for user experience, IMHO.

I hope this answers your question.

这篇关于在加入Android的动态复选框preferences,并在preference屏幕上显示出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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