从零开始学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;}}

展示效果: