如何在Android上动态显示和隐藏首选项?
有没有一种方法可以动态显示和隐藏首选项?就我而言,我有一个复选框首选项,它将禁用或启用2个首选项组(有-"和无障碍"组)之一.虽然这将是台式机环境中的理想GUI,但有障碍"几乎占据了整个屏幕,而其他无障碍"仅占据了屏幕的一小部分.
Is there a way to dynamically show and hide preferences? In my case, I have a checkbox preference that would disable or enable one of 2 preference groups ("with-" and "without-handicap" groups). While this would be the ideal GUI in a desktop environment, the "with-handicap" takes up nearly the whole screen, while the other, "without-handicap" takes up only a small portion of the screen.
我不想一次显示两个组,而是一次只显示其中一个,并在复选框更改时动态显示或隐藏这两个组.有办法吗?
Rather than showing both groups at the same time, I'd like to show only one of them at a time, and dynamically show or hide the 2 groups when the checkbox changes. Is there a way to do this?
通过PreferenceActivity调用
From a PreferenceActivity call
Preference somePreference = findPreference(SOME_PREFERENCE_KEY);
PreferenceScreen preferenceScreen = getPreferenceScreen();
preferenceScreen.removePreference(somePreference);
您以后可以致电:
preferenceScreen.addPreference(somePreference);
仅有一点棘手的部分是重新添加时使顺序正确.请查看 PreferenceGroup 了解详情.
The only a little bit tricky part is getting the order correct when adding back in. Look at PreferenceScreen documentation, particularly it's base class, PreferenceGroup for details.
注意:以上内容仅适用于PreferenceScreen
的直系子代.如果两者之间有一个PreferenceCategory
,则需要从其父级PreferenceCategory
而不是PreferenceScreen
中删除该首选项.首先确保PreferenceCategory
在XML文件中设置了android:key
属性.然后:
Note: The above will only work for immediate children of a PreferenceScreen
. If there is a PreferenceCategory
in between, you need to remove the preference from its parent PreferenceCategory
, not the PreferenceScreen
. First to ensure the PreferenceCategory
has an android:key
attribute set in the XML file. Then:
Preference somePreference = findPreference(SOME_PREFERENCE_KEY);
PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference(SOME_PREFERENCE_CATEGORY_KEY);
preferenceCategory.removePreference(somePreference);
和:
preferenceCategory.addPreference(somePreference);