从零开始学android:Android事件处理—下拉列表框与OnItemSelectedListener
来源:互联网 发布:php 参数传递 编辑:程序博客网 时间:2024/06/04 23:27
下拉列表监听
Spinner组件的主要功能是用于进行下拉列表显示的功能,当用户选中下拉列表中的某个选项之后可以使用Spinner类中提供的setOnItemClickListener()方法进行监听。
范例:联动菜单实现
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Spinner android:id="@+id/city" android:prompt="@string/city_prompt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/city_labels" /> <Spinner android:id="@+id/area" android:prompt="@string/area_prompt" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
字符串文件:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">onItemSelectedListener</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="city_prompt">选择你喜欢的城市:</string> <string name="area_prompt">选择你喜欢的城区:</string></resources>
数组文件:
<?xml version="1.0" encoding="utf-8"?><resources> <array name="city_labels"><item>中国 - 北京</item><item>中国 - 上海</item><item>中国 - 广州</item> </array></resources>
程序文件:
package com.richard.onitemselectedlistener;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Spinner;public class MainActivity extends Activity {private Spinner city = null;// 定义下拉列表框private Spinner area = null;// 定义下拉列表框private String[][] areaData = new String[][] {// 定义联动菜单项{ "东城", "西城", "朝阳", "大兴", "平谷" }, // 第一级子选项{ "黄浦", "杨浦", "闵行" }, // 第二级子选项{ "广州" } };// 第三级子选项private ArrayAdapter<CharSequence> adapterArea = null; // 下拉列表内容适配器@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 父类onCreate()super.setContentView(R.layout.activity_main);// 调用布局管理器this.city = (Spinner) super.findViewById(R.id.city);// 取得组件this.area = (Spinner) super.findViewById(R.id.area);// 取得组件this.city.setOnItemSelectedListener(new OnItemSelectedListenerImpl());// 设置监听器 }private class OnItemSelectedListenerImpl implements OnItemSelectedListener {@Overridepublic void onItemSelected(AdapterView<?> adapterView, View view,int position, long id) {// 选项选中时触发MainActivity.this.adapterArea = new ArrayAdapter<CharSequence>(MainActivity.this,android.R.layout.simple_spinner_item,MainActivity.this.areaData[position]);// 实例化列表项MainActivity.this.adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 设置下拉列表显示风格MainActivity.this.area.setAdapter(MainActivity.this.adapterArea);// 设置数据}@Overridepublic void onNothingSelected(AdapterView<?> adapterView) {// 没有选项时触发}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
展示效果:
- 从零开始学android:Android事件处理—下拉列表框与OnItemSelectedListener
- 第21章、OnItemSelectedListener事件(从零开始学Android)
- 第21章、OnItemSelectedListener事件(从零开始学Android)
- 第21章、OnItemSelectedListener事件(从零开始学Android)
- 第22章、OnItemSelectedListener事件与二级联动(从零开始学Android)
- 第22章、OnItemSelectedListener事件与二级联动(从零开始学Android)
- 第22章、OnItemSelectedListener事件与二级联动(从零开始学Android)
- 从零开始学android:Android事件处理—单选钮与OnCheckedChangeListener
- 从零开始学android<Spinner下拉列表框的使用.九.>
- 从零开始学android:Android事件处理—单击事件
- 从零开始学android:Android事件处理—焦点事件
- 从零开始学android:Android事件处理—长按事件
- 从零开始学android:Android事件处理—键盘事件
- 从零开始学android:Android事件处理—触摸事件
- Android——OnItemSelectedListener事件与二级联动
- 从零开始学android:Android事件处理—监听日期与时间的改变
- 第8章、下拉列表框Spinner(从零开始学Android)
- 第8章、下拉列表框Spinner(从零开始学Android)
- const与define
- 【第一章】证券交易概述_整理
- 知识图谱技术的演进
- linux线程使用读写锁共享全局变量
- Android项目之旅二 简易Mp3播放器页面设计
- 从零开始学android:Android事件处理—下拉列表框与OnItemSelectedListener
- TClientDataSet中关于TField、TFieldDef动态创立字段的应用
- 使用源代码将 Glibc 升级到 2.6 ;已成功验证,非常好
- 模型Skinned Mesh在某些情况下不显示的解决
- 第一章 SQL Server 数据库基础复习内容(上机)
- 测试本机的大小端模式
- javascript 深拷贝
- Mac OS X日常操作
- opencv中的矩阵操作