如何隐藏屏幕上的偏好

时间:2014-04-24 07:34:11

标签: android

我有一个班级Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener。我正在从xml文件加载首选项。但在某些情况下,我需要根据我的应用状态完全隐藏其中一个首选项屏幕。我想完全从屏幕中删除该首选项。可能吗 ?我只需要settings_account和所有其他隐藏

<Preference
            android:title="Account Settings"
             android:key="customPref" />

<PreferenceScreen android:title="@string/account_1"
                android:key="account">
        <EditTextPreference
            android:key="username"
            android:title="@string/settings_username"
            android:singleLine="true" />
        <EditTextPreference
            android:key="password"
            android:title="@string/settings_password"
            android:password="true"
            android:singleLine="true" />
            </PreferenceScreen>


<PreferenceScreen android:title="@string/account_2"
            android:key="account1">
        <EditTextPreference
            android:key="username1"
            android:title="@string/settings_username"
            android:singleLine="true" />
        <EditTextPreference
            android:key="password1"
            android:title="@string/settings_password"
            android:password="true"
            android:singleLine="true" />
            </PreferenceScreen>

    android:title="@string/settings_pbxes_options">
        <CheckBoxPreference
            android:key="par"
            android:title="@string/settings_par"
            android:summary="@string/settings_par2" />
        <CheckBoxPreference
            android:key="improve"
            android:title="@string/settings_improve"/>
        <EditTextPreference
            android:key="posurl"
            android:title="@string/settings_posurl"
            android:hint="https://www1.pbxes.com/name"
            android:singleLine="true" />
        <CheckBoxPreference
            android:key="pos"
            android:title="@string/settings_pos"
            android:summary="@string/settings_pos2" />
        <CheckBoxPreference
            android:key="callback"
            android:title="@string/settings_callback"
            android:summary="@string/settings_callback2" />
        <CheckBoxPreference
            android:key="callthru"
            android:title="@string/settings_callthru"
            android:summary="@string/settings_callback2" />
        <EditTextPreference
            android:key="callthru2"
            android:title="@string/settings_callthru2"
            android:hint="Accessno.,PIN#"
            android:singleLine="true" />
</PreferenceScreen> 

2 个答案:

答案 0 :(得分:0)

也许您可以创建第二个首选项xml并改为加载它。

答案 1 :(得分:0)

如果您的首选项位于PreferenceCategory中,请执行以下操作 XML:

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

<CheckBoxPreference
    android:key="checkPref" />

爪哇:

CheckBoxPreference mCheckBoxPref = (CheckBoxPreference) findPreference("checkPref");
PreferenceCategory mCategory = (PreferenceCategory) findPreference("category_foo");
mCategory.removePreference(mCheckBoxPref);

或者你可以这样做:

myPreferenceScreen.removePreference(myPreference);