android开发中PreferenceScreen的使用注意事项

发布时间:2025-12-09 18:50:47 浏览次数:4

今天开发中需要用到android自带的偏好功能PreferenceScreen 在网上教程很多刚开始设置为

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="设置">
        <EditTextPreference
            android:defaultValue="/mnt/sdcard/歌曲存放目录"
            android:summary="歌曲默认存放位置"
            android:title="存储目录"></EditTextPreference>
        <CheckBoxPreference
            android:summary="是否保存播放进度"
            android:title="保存播放进度"></CheckBoxPreference>
    </PreferenceCategory>

打开设置页面以后 无论怎么弄都不会自动保存数据 而且data目录下也没有生成相应的配置文件

后来看了下标签的所有参数 发现有个android:key参数 想了想 这东西是按照键值对方式储存的

是不是没有key的话就不能储存呢? 于是给android:key一个值,设置竟然可以保存了,后来代码为

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="设置">
        <EditTextPreference
            android:defaultValue="/mnt/sdcard/歌曲存放这里"
            android:key="song_path"
            android:summary="歌曲默认存放位置"
            android:title="存储目录"></EditTextPreference>
        <CheckBoxPreference
            android:defaultValue="true"
            android:key="song_playsave"
            android:summary="是否保存播放进度"
            android:title="保存播放进度"></CheckBoxPreference>
    </PreferenceCategory>
</PreferenceScreen>

这个留存作参考

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477