zoukankan      html  css  js  c++  java
  • Preference dependency 和 disableDependentsState属性 小结



    1.Activity代码如下:


    public class ConfigActivity extends PreferenceActivity {  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            addPreferencesFromResource(R.xml.config);  
        }  
    }



    2.Config.xml代码如下:

    <?xml version="1.0" encoding="utf-8"?>  

    <PreferenceScreen  
      xmlns:android="http://schemas.android.com/apk/res/android">  
            <PreferenceCategory android:key="config" android:title="whConfig">  
            <CheckBoxPreference android:key="config_anonymous"   
                android:title="config_anonymous"  
                android:disableDependentsState="true" />  
            <EditTextPreference android:key="config_username"   
                android:title="config_username"  
                android:dependency="config_anonymous" />  
            <EditTextPreference android:key="config_password"   
                android:title="config_password"  
                android:dependency="config_anonymous" />  
        </PreferenceCategory>
    </PreferenceScreen> 


    3、实现的效果:


    (解释:当上面的checkbox值为真,禁用下面的用户名和密码设置,否则开启下面的设置项)

     

    4、关键点:

    注意上面代码中的黑色粗体代码,android:dependency="config_anonymous"这个属性指定该设置项依赖于前面key="config_anonymous" 的

    CheckBoxPreference的值,如果config_anonymous的值为真(即选中状态),则config_username和config_password可用,即enabled的,

    否则为disabled。但是,现在的逻辑刚好相反,即config_anonymous的值为真的时候,需要禁用下面的config_username和config_password设置项。

    要实现这个就需要在CheckBoxPreference上设置了,即添加android:disableDependentsState="true"这个属性,这个属性的意思是,当CheckBoxPreference的值为真的时候,禁用依赖于该设置的其他设置项。



  • 相关阅读:
    idea2020 安装
    739. 每日温度
    图像翻转
    257. 二叉树的所有路径
    1466. 重新规划路线
    面试题 04.05. 合法二叉搜索树
    671. 二叉树中第二小的节点
    965. 单值二叉树
    648. 单词替换
    137. 只出现一次的数字 II
  • 原文地址:https://www.cnblogs.com/liulaolaiu/p/11744706.html
Copyright © 2011-2022 走看看