微调框

微调框提供一种方法,让用户可以从值集中快速选择一个值。默认状态下,微调框显示其当前所选的值。 触摸微调框可显示下拉菜单,其中列有所有其他可用值,用户可从中选择一个新值。

您可以使用 Spinner 对象向您的布局中添加一个微调框。通常应在 XML 布局中使用 元素来执行此操作。 例如:

<Spinner
    android:id="@+id/planets_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

要使用选择列表填充微调框,还需要在 Activity 或 Fragment 源代码中指定 SpinnerAdapter。

使用用户选择填充微调框

您为微调框提供的选择可来自任何来源,但必须通过 SpinnerAdapter 来提供,例如,如果选择可通过数组获取,则通过 ArrayAdapter)来提供,如果选择可通过数据库查询获取,则通过 CursorAdapter 来提供。

例如,如果预先确定了微调框的可用选择,则可通过字符串资源文件中定义的字符串数组来提供这些选择。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
        <item>Jupiter</item>
        <item>Saturn</item>
        <item>Uranus</item>
        <item>Neptune</item>
    </string-array>
</resources>

对于上例所示数组,可在 Activity 或 Fragment 中使用以下代码,以使用 ArrayAdapter 实例为微调框提供该数组。

Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

createFromResource() 方法允许从字符串数组创建 ArrayAdapter。 此方法的第三个参数是布局资源,其定义所选选择如何显示在微调框控件中。 simple_spinner_item 布局由平台提供,是默认布局,除非您想为微调框外观定义自己的布局,否则应使用此布局。

然后,应调用 setDropDownViewResource(int) 指定适配器应用于显示微调框选择列表的布局(simple_spinner_dropdown_item 是平台定义的另一标准布局)。

调用 setAdapter() 以将适配器应用到 Spinner。

响应用户选择

当用户从下拉菜单中选择一个项目时,Spinner 对象会收到一个 on-item-selected 事件。

要为微调框定义选择事件处理程序,请实现 AdapterView.OnItemSelectedListener 接口以及相应的 onItemSelected() 回调方法。例如,以下是 Activity 中的一个接口实现:

public class SpinnerActivity extends Activity implements OnItemSelectedListener {
    ...

    public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}

AdapterView.OnItemSelectedListener 需要 onItemSelected() 和 onNothingSelected() 回调方法。

然后,您需要调用 setOnItemSelectedListener() 来指定接口实现:

Spinner spinner = (Spinner) findViewById(R.id.spinner);

spinner.setOnItemSelectedListener(this);

如果您使用 Activity 或 Fragment 来实现 AdapterView.OnItemSelectedListener 接口(如上例),则可以传递 this 作为接口实例。

我的微信二维码如下,欢迎交流讨论

欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧

微信订阅号二维码如下:

Android的Spinner控件用法解析的更多相关文章

  1. android之Spinner控件用法

    用法1: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...

  2. android 学习 Spinner控件的使用

    今晚看了下spinner控件的使用,结合博客大神的教程,一个小demo 一,SpinnerActivity private Spinner spinner; private ArrayAdapter& ...

  3. android之RatingBar控件用法

    MainActivity.java package com.example.mars_2500_ratingbar; import android.support.v7.app.ActionBarAc ...

  4. android之SeekBar控件用法

    MainActivity.java package com.example.mars_2400_seekbar; import android.support.v7.app.ActionBarActi ...

  5. android之AutoCompleteTextView控件用法

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  6. android之datepicker控件用法

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. Android spinner控件

    spinner控件是Android中下拉控件,现在介绍它两种用法.第一种,从资源文件中获取下拉值:第二种,从代码中获取下拉值. 第一种,首先要在资源文件中把值写好: <?xml version= ...

  8. android Spinner控件详解

    Spinner提供了从一个数据集合中快速选择一项值的办法.默认情况下Spinner显示的是当前选择的值,点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner ...

  9. 大约Android PopupWindow有用Spinner控件点击APP Crash案例整理!

    场景异常,如下面: android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.V ...

随机推荐

  1. web前端HTML基础

    一.HTML介绍 HTML全称是(Hypertext Markup Language, HTML)又称为超级文本标记语言,它主要his一种用于创建网页的标记语言,在本质上是浏览器可以识别的规则,我们按 ...

  2. Asp.net Core2.0 缓存 MemoryCache 和 Redis

    自从使用Asp.net Core2.0 以来,不停摸索,查阅资料,这方面的资料是真的少,因此,在前人的基础上,摸索出了Asp.net Core2.0 缓存 MemoryCache 和 Redis的用法 ...

  3. [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

  4. js if for 详解 获取元素方式 及一些js 基础知识

    ##获取元素的新方法## --document.querySelector('Css Selector{css选择器}') 接收一个css选择器(通配,群组,类,包含,id....等) 若这个选择器对 ...

  5. pyqt5 动画学习(四) 旋转动画,使用QGraphicsView让自己的控件旋转起来

    今天学有所成,赶紧记下今天的成果 之前三篇文章分别演示了空间的大小改变,移动,及颜色变化.在后续研究旋转的过程中即为艰难 如果你是使用pyqt4,那么使用QGraphicsItemAnimation便 ...

  6. [SDOI2016]排列计数

    Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是 ...

  7. C++Primer学习——各种运算符

    前缀递增和后缀递增 class NewInt { public: NewInt():RootInt(0){}; NewInt(int IniInt):RootInt(IniInt){}; NewInt ...

  8. SPOJ Query on a tree V

    You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are number ...

  9. Saltstack基础

    salt介绍 salt简单介绍 一个基础平台管理工具 一个配置管理系统,能够维护预定义状态的远程节点 一个分布式远程执行系统,用来在远程节点上执行命令和查询数据 salt的核心功能 使命令发送到远程系 ...

  10. (转)SQL中的循环、for循环、游标

    我们使用SQL语句处理数据时,可能会碰到一些需要循环遍历某个表并对其进行相应的操作(添加.修改.删除),这时我们就需要用到咱们在编程中常常用的for或foreach,但是在SQL中写循环往往显得那么吃 ...