Android组件之Spinner(下拉列表)

2024-04-17 03:18
文章标签 android 组件 列表 spinner

本文主要是介绍Android组件之Spinner(下拉列表),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android组件之Spinner(下拉列表)

分类: Android之路 218人阅读 评论(0) 收藏 举报

Spinner是一个列表选择框,但其并不需要显示下拉列表,二十相当于一个菜单供用户选择,下面用一个例子介绍:

在样式文件中:

[html] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <TextView   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="请选择一项运动项目"  
  11.         />  
  12.     <Spinner   
  13.     android:id ="@+id/sportsSp"  
  14.     android:layout_width="match_parent"  
  15.     android:layout_height="wrap_content"  
  16.     android:prompt="@string/spinner_prompt"  
  17.     android:entries="@array/sports"  
  18.     />  
  19.       
  20. </LinearLayout>  


在<Spinner>标签中,通过android:prompt来设置弹出选择框的标题,通过android:entries来设置默认的列表选项(定义在一个arrays.xml文件中)

[html] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string-array name="sports" >  
  4.         <item>足球</item>  
  5.         <item>篮球</item>  
  6.         <item>乒乓球</item>  
  7.         <item>网球</item>       
  8.     </string-array>  
  9. </resources>  

监听列表点击事件:

[html] view plain copy print ?
  1. package cn.class3g.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.widget.AdapterView;  
  8. import android.widget.AdapterView.OnItemSelectedListener;  
  9. import android.widget.Spinner;  
  10. import android.widget.TextView;  
  11.   
  12. public class SpinnerDemo extends Activity implements OnItemSelectedListener{  
  13.     Spinner sportSp = null;  
  14.       
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.spinner_layout);  
  18.         findViews();  
  19.     }  
  20.   
  21.     private void findViews() {  
  22.         sportSp = (Spinner)this.findViewById(R.id.sportsSp);  
  23.         sportSp.setOnItemSelectedListener(this);  
  24.         sportSp.performClick();  
  25.     }  
  26.     //每选择一次均以日志输出形式打印  
  27.     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
  28.             long arg3) {  
  29.         TextView tv = (TextView) arg1;  
  30.         Log.i("TAG", tv.getText().toString());  
  31.     }  
  32.   
  33.     public void onNothingSelected(AdapterView<?> arg0) {  
  34.     }  
  35. }  

在模拟器中的效果与日志输出结果:

这篇关于Android组件之Spinner(下拉列表)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/910662

相关文章

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python中列表应用和扩展性实用详解

《python中列表应用和扩展性实用详解》文章介绍了Python列表的核心特性:有序数据集合,用[]定义,元素类型可不同,支持迭代、循环、切片,可执行增删改查、排序、推导式及嵌套操作,是常用的数据处理... 目录1、列表定义2、格式3、列表是可迭代对象4、列表的常见操作总结1、列表定义是处理一组有序项目的

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解