目录(?)[-]

  1. XML文件
  2. 在设备中保存
  3. 读出信息

ListPreference提供单选列表,我们可以通过CheckBoxPreference提供多选列表。此外,Android在3.0后提供MultiSelectListPreference,它的实现和ListPreference相似,不同的是还可以不选择或者多个选择。如图所示。

XML文件

在复合例子中加入相关的内容,如下。同样地可以采用@array来对entry和entryvalue进行定义,使用比CheckBox Preference要方便。

...... 
  <PreferenceScreen android:key="multi_select_test" 
    android:title="MultiSelect List Preference" 
    android:summary="Test for Multiselect List Preferences"> 
      <MultiSelectListPreference android:key="fruits" 
        android:title="Select Your Favourite Fruits" 
        android:summary="选择您喜欢的水果" 
        android:entries="@array/fruit" 
        android:entryValues="@array/fruit_index" 
        android:dialogTitle="水果:"  />     
  </PreferenceScreen> 
… …

在设备中保存

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map> 
    … …  
    <set name="fruits"> 
        <string>2</string> 
        <string>0</string> 
    </set> 
    … … 
</map>

读出信息

从保存文件中可以看出,数据格式是Set,读取起来比以往的稍微麻烦一点,代码如下:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
Set<String>options = prefs.getStringSet("fruits", null);  //因不设置初始值,所以要进行null的判断 
showInfo("Multi Select: " + options); 
String[] fruits = getResources().getStringArray(R.array.fruit); 
if(options != null ){
    for(String f: options){  
        showInfo("select " + fruits[Integer.parseInt(f)]);
    } 
}

本博文涉及的例子代码,可以在Pro Android学习:Preference(首选项)小例子中下载。

相关链接: 我的Android开发相关文章

【转】 Pro Android学习笔记(六十):Preferences(4):MultiSelect List Preference的更多相关文章

  1. 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView

    目录(?)[-] 点击List的item触发 添加其他控件以及获取item数据 ListView控件以垂直布局方式显示子view.系统的android.app.ListActivity已经实现了一个只 ...

  2. 【转】Pro Android学习笔记(十二):了解Intent(下)

    解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...

  3. 【转】Pro Android学习笔记(十):了解Intent(上)

    目录(?)[-] Intent基本含义 系统的Intent Android引入了Intent的概念来唤起components,component包括:1.Activity(UI元件) 2.Servic ...

  4. 【转】Pro Android学习笔记(十四):用户界面和控制(2):Text类控制

    目录(?)[-] TextView 例子1在XML中设置autoLink属性 例子2在代码中设置autoLink属性 EditText AutoCompleteTextView MultiAutoCo ...

  5. 【转】Pro Android学习笔记(十六):用户界面和控制(4):ImageView控件

    目录(?)[-] XML片段 代码设置ImageView ImageView是基础的控件,它是android.widget.ImageView的继承类. XML片段      <LinearLa ...

  6. 【转】Pro Android学习笔记(十八):用户界面和控制(6):Adapter和AdapterView

    目录(?)[-] SimpleCursorAdapter 系统预置的layout ArrayAdapter 动态数据增插删排序 自定义TextView风格 其他Adapter AdapterView不 ...

  7. 【转】Pro Android学习笔记(十五):用户界面和控制(3):Button控件

    目录(?)[-] 基础Button ImageButton ToggleButton CheckBox RadioButton 基础Button Button是最常用的基础控件之一,在Android中 ...

  8. 【转】Pro Android学习笔记(三十):Menu(1):了解Menu

    目录(?)[-] 创建Menu MenuItem的属性itemId MenuItem的属性groupId MenuItem的属性orderId MenuItem的属性可选属性 Menu触发 onOpt ...

  9. 【转】 Pro Android学习笔记(三五):Menu(6):XML方式 & PopUp菜单

    目录(?)[-] 利用XML创建菜单 XML的有关属性 onClick事件 Pop-up菜单 利用XML创建菜单 在代码中对每个菜单项进行设置,繁琐且修改不灵活,不能适配多国语言的要求,可以利用资源进 ...

  10. 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout

    目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...

随机推荐

  1. Netty 高并发 (长文)

    目录 Netty+Zookeeper 亿级 高并发实战 (长文) 写在前面 1. 高并发IM架构与部分实现 1.1. 高并发的学习和应用价值 1.1.1. 高并发IM实战的价值 1.1.2. 高并发I ...

  2. python爬虫之Selenium

    Selenium的使用 #!/usr/bin/env python # -*- coding:utf-8 -*- """ Selenium是一个第三方模块,可以完全模拟用 ...

  3. java面向对象入门之带参方法创建

    /* Name :创建带参的方法 Power by :Stuart Date:2015.4.25 */ //创建Way类 class Way{ //Way类成员的基本变量 int add1=123; ...

  4. 电脑Svchost.exe 进程占CPU100% 的解决办法

    Windows Update诊断和修复修复工具 http://support.microsoft.com/mats/windows_update/zh-cn Svchost.exe占用CPU100%的 ...

  5. Linux里AWK中split函数的用法

    跟java里的split函数的用法是很相像的,举例如下: The awk function split(s,a,sep) splits a string s into an awk array a u ...

  6. 【leetcode刷题笔记】Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模 ...

  7. hd acm2045

    LELE的RPG难题 析: 假设有N个方格时的涂法是F[N]种.当前边n-1个方格成立时,再加第n种颜色无影响,此时有F[N-1]种涂法,当n-1个方格违法时,即有两个相邻的格子颜色相同,则有n-2个 ...

  8. shell 查看系统有关信息

    磁盘: 查看磁盘空间或者挂载情况 df -ah 或者 df -h 内存: 查看内存使用情况 free -m total used free shared buffers cached Mem: -/+ ...

  9. JavaScript的Function 类型

    一,Function定义 Function实际上是对象,与其他引用类型一样具有属性和方法.Function可以通过三种方法进行定义,分别是函数声明语法定义,函数表达式定义和Function构造函数定义 ...

  10. ubuntu 部署的mysql无法远程链接

    允许远程用户登录访问mysql的方法 从任何主机上使用root用户,密码:youpassword(你的root密码)连接到mysql服务器: # mysql -u root -proot mysql& ...