在开发软件设置界面的时候,我们可以采用android系统提供的PreferenceActivity来实现,下面给出一个简单的例子:
1、Activity 代码如下:
public class ConfigActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.config);
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
}
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="@string/config">
<CheckBoxPreference android:key="config_anonymous"
android:title="@string/config_anonymous"
android:disableDependentsState="true"/>
<EditTextPreference android:key="config_username"
android:title="@string/config_username"
android:dependency="config_anonymous"/>
<EditTextPreference android:key="config_password"
android:title="@string/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的值为真的时候,禁用依赖于该设置的其他设置项。
- 大小: 14.1 KB
分享到:
相关推荐
android Preference自定义样式
implementation 'moe.shizuku.preference:preference-dialog-android:' //implementation 'moe.shizuku.preference:preference-dialog-appcompat:' // if you want to use appcompat version dialog //...
上篇博文代码实现了Android自带的preference组件,本文将通过实例讲解自定义preference组件。 主要通过以下几步来实现: 1.定义需要的layout布局res->layout->xml文件; 2.通过继承Preference类,来实现自定义...
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <Preference android:key="settings" android:title="@string/settings" > android:targetClass=...
Preference组件是android的SharePreferences的衍生品,Preference组件的状态值是默认永久的保存在/data/data/包名/shared_prefs 目录下,因为Android系统的Setting应用及一些符合Android设计思想的应用的设置界面...
Android中Preference的使用以及监听事件分析
Android中preference的使用,使用详解.
Android提供preference这个键值对的方式来处理这种情况,自动保存这些数据,并立时生效,同时Android提供一种类似的layout的方式来进行Preference的布局。 Preference组件有ListPreference,EditTextPreference,...
Android Preference Framework源码~~~~~~~~~~~~~~~~~
此DEMO内含基本的android preference framework的简单介绍,包括CheckboxPreference, RingtonePreference, EditTextPreference以及ListPreference。主要探究了一下android 怎么通过使用preference从而达到对用户定制...
implementation( " net.xpece.android:support-preference:3.0.0 " ) } repositories { mavenCentral() } 兼容性 3.xx版需要AndroidX。 如何也获得颜色选择器的首选项? dependencies { implementation( " ...
android程序的一个demo,演示Preference保存设置的功能
安卓Android源码——Preference_Demo.rar
这是Pro Android学习系列中preference部分的例子源代码。
1.添加gradle依赖将此添加到您的gradle文件中: implementation " androidx.preference:preference: $a ndroidxVersion "implementation ' com.takisoft.preferencex:preferencex:1.1.0 '2.使用适当的类作为片段的...
NULL 博文链接:https://sunney2012.iteye.com/blog/1158214
android-support-v7-preference.jar
导语:PreferenceActivity是一个方便设置管理的界面,但是对于界面显示来说比较单调,所以自定义布局就很有必要了。本文举例说明在Preference中自定义layout的方法。...Preference android:layout=”@layout/youmi_ad”
Material Preference Library uses com.android.support:preference-v7:x.x.x support library widgets. Also it includes a color chooser dialog widget, that can be used to select accent color . Header is ...
Android preference activity sample.