如何从preferenceActivity一个preference?preferenceActivity、preference

2023-09-05 06:07:54 作者:伸手

我用preferenceActivity。我如何删除preference?我似乎无法得到这个工作:

I'm using PreferenceActivity. How do I remove a preference? I cannot seem to get this to work:

Preference p = findPreference("grok");
boolean worked = getPreferenceScreen().removePreference(p);
// worked == false.

所以preference被发现,但删除preference()调用失败。什么是正确的方式做到这一点?我使用的是preference.xml文件,像这样的按键:

So the preference is found, but the removePreference() call fails. What's the right way to do this? I'm using a preference.xml file for the keys like so:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="foo">

        <CheckBoxPreference
            android:key="grok" />

            ...

感谢

推荐答案

您只能删除确切孩子在preferenceGroup。所以,你的情况,你应该添加一些关键preferenceCategory(与标题=富),然后找到preference用此键和放大器;然后将其删除子

you can remove only exact child in PreferenceGroup. So in your case, you should add some key to PreferenceCategory (with title="foo"), then findPreference with this key & then remove it child

XML:

<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory
    android:key="category_foo"
    android:title="foo">

    <CheckBoxPreference
        android:key="grok" />

        ...

code:

Code:

Preference p = findPreference("grok");
// removing Preference
((PreferenceGroup) findPreference("category_foo")).removePreference(p);