版权声明:本文为博主原创文章,未经博主允许不得转载。
http://blog.csdn.net/wh_19910525/article/details/8517473
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的值为真的时候,禁用依赖于该设置的其他设置项。
顶 0 踩 0 上一篇android背景选择器selector用法小结 下一篇android 4.1 "Mobile networks" 选项 不存在