[置顶] 一步一步学android之事件篇——下拉列表事件
上一篇RadioGroup比较简单,所以再学习个spinner的OnItemSelectedListener事件,前面说过spinner的主要功能就是提供列表显示的选择,比如我们在选择城市的时候就会用到spinner(网页中更加常见),在要获取选择内容时就要用到OnItemSelectedListener来监听获取。下面同样用例子来说明。
运行效果如下:
同样在values下面新建一个xml文件(Spinner组件篇有),city_spinner:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="city"> <item>北京</item> <item>上海</item> <item>广州</item> </string-array> </resources>
在strings.xml文件中添加:
<string name="prompt">请选择您喜欢的城市...</string>
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/showInfo" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:prompt="@string/prompt" android:entries="@array/city"/> </LinearLayout>
MainActivity.java:
package com.example.onitemselectedlistenerdemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends Activity { private Spinner spinner = null; private TextView showInfo = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView(){ spinner = (Spinner)super.findViewById(R.id.spinner); showInfo = (TextView)super.findViewById(R.id.showInfo); spinner.setOnItemSelectedListener(new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub showInfo.setText("您喜欢的城市是:"+parent.getItemAtPosition(position).toString()); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); } }
这里说下上面这句
parent.getItemAtPosition(position).toString()
是获取在position位置上的内容,然后转换成String类型传给showInfo显示。
这个城市选择了,那么要区级选择怎么办?下面要实现的就是一个比较常见的功能——联动菜单(其实就是在第一个选择之后,第二个选择的内容列表只与第一个有关,比如广州下面有白云区,越秀区等等,北京有朝阳区,西城区等等,选择北京只出现北京的对应区,这就是联动),下面开始实现(在上面例子的基础上加些代码就可以了)。
效果如下:
strings.xml加下面这行代码:
<string name="promptc">请选择您喜欢的城区...</string>
main.xml改为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/showInfo" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:prompt="@string/prompt" android:entries="@array/city"/> <Spinner android:id="@+id/spinnerc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:prompt="@string/promptc"/> </LinearLayout>
MainActivity.java改为:
package com.example.onitemselectedlistenerdemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends Activity { private Spinner spinner = null; private Spinner spinnerc = null; private TextView showInfo = null; //定义联动菜单项 private String[][] area = new String[][]{ {"东城","西城","朝阳","大兴"}, {"黄浦","杨浦"}, {"白云","越秀","南沙"} }; //二级菜单适配器 private ArrayAdapter<CharSequence> areaAdapter = null; private String cString = ""; private String aString = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView(){ spinner = (Spinner)super.findViewById(R.id.spinner); spinnerc = (Spinner)super.findViewById(R.id.spinnerc); showInfo = (TextView)super.findViewById(R.id.showInfo); spinner.setOnItemSelectedListener(new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub cString = parent.getItemAtPosition(position).toString(); //实例化列表 areaAdapter = new ArrayAdapter<CharSequence>(MainActivity.this, android.R.layout.simple_spinner_item,area[position]); //设置显示风格 areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //装载数据 spinnerc.setAdapter(areaAdapter); spinnerc.setOnItemSelectedListener(new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub aString = parent.getItemAtPosition(position).toString(); showInfo.setText("您喜欢的城市为:"+cString+" "+"地区为:"+aString); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } }
这个事件唯一要注意的是在Spinner第一次加载的时候是会调用一次OnItemSelectedListener这个事件的,好了,今天就到这里了。