上一篇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这个事件的,好了,今天就到这里了。

[置顶] 一步一步学android之事件篇——下拉列表事件的更多相关文章

  1. 一步一步学android之事件篇——单击事件

    在使用软件的时候单击事件必不可少,比如我想确定.取消等都需要用户的单击,所有的单击事件都是由View.OnClickListener接口来进行处理的,接口定义如下: public static int ...

  2. 一步一步学android之事件篇——触摸事件

    触摸事件顾名思义就是触摸手机屏幕触发的事件,当用户触摸添加了触摸事件的View时,就是执行OnTouch()方法进行处理,下面通过一个动态获取坐标的例子来学习OnTouchListener事件,效果如 ...

  3. [置顶] 新修改ADB,支持Android 4.2 系统 ,全部中文命令,手机屏幕截图等等

    发过好几个ADB的工具,有很多朋友用了之后给我反馈了不少的意见和bug,这里非常感谢他们,所以今天花了一天的时间重新整理了一下ADB,并且修改了这些BUG.也有朋友建议我给一个修改列表,今天发这个帖子 ...

  4. [置顶] High Performance Canvas Game for Android

    Rule #0 为移动平台进行优化 为移动平台进行优化是十分重要的,因为移动平台的性能大概只有桌面平台的1/10左右(*1),它通常意味着: 更慢的CPU速度,这意味着不经过优化的JavaScript ...

  5. [置顶] 和孩子们一起学Python编程

    1. 推荐书名 Computer Programming for Kids and Other Beginners in Python, 4Ed.pdf     中文译名:<和孩子们一起学Pyt ...

  6. [置顶]【实用 .NET Core开发系列】- 导航篇

    前言 此系列从出发点来看,是 上个系列的续篇, 上个系列因为后面工作的原因,后面几篇没有写完,后来.NET Core出来之后,注意力就转移到了.NET Core上,所以再也就没有继续下去,此是原因之一 ...

  7. 自定义置顶TOP按钮

    简述一下,分为三个步骤: 1. 添加Html代码 2. 调整Css样式 3. 添加Jquery代码 具体代码如下: <style type="text/css"> #G ...

  8. 一步一步学android控件(之十五) —— DegitalClock & AnalogClock

    原本计划DigitalClock和AnalogClock单独各一篇来写,但是想想,两个控件的作用都一样,就和在一起写一篇了. DegitalClock和AnalogClock控件主要用于显示当前时间信 ...

  9. 一步一步学android控件(之十六)—— CheckBox

    根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式.今天主要学习如何自定义CheckBox样式.在CheckBox状态改变时有时需要做一些 ...

随机推荐

  1. 基于visual Studio2013解决面试题之0710求方优化

     题目

  2. 张佩的Dump服务

    [亦请参考: http://www.yiiyee.cn/Blog/dumpservice/ ] 张佩提供 有偿但 价格极低的Dump文件分析服务 ! . 如果你有一个Dump文件——不管是应用程序还是 ...

  3. 如何使用Gmail的别名功能?

    Gmail真的是一个很好的邮箱,一直是我的最爱!它有一个很独特的功能-别名,用这个功能,我们就可以把一个 邮箱当成很多个邮箱来使用了! 1.Gmail 不提供传统别名 ,但是你可以收到发送到 your ...

  4. delphi中无类型文件读写

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  5. TWinControl.WMNCPaint对非客户的绘制

    混个脸熟: procedure TWinControl.WMNCPaint(var Message: TMessage); const InnerStyles: , BDR_SUNKENINNER, ...

  6. linux kill进程和子进程小trick

           我们的hive web是调用polestar restful service(https://github.com/lalaguozhe/polestar-1)来执行具体的hive或者s ...

  7. 中国本土管理咨询公司排名TOP50

    中国本土管理咨询公司排名TOP50 1. 北京正略钧策管理顾问有限公司 2. 北京和君咨询公司 3. 北大纵横管理咨询公司 4. 远卓管理顾问公司 5. AMT管理咨询公司 6. 华夏基石管理咨询有限 ...

  8. C++基础之二:常量指针和指针常量

    1.常量指针 定义:具有只能够读取内存中数据,却不能够修改内存中数据的属性的指针,称为指向常量的指针,简称常量指针. 声明:const int * p; 注:可以将一个常量的地址赋值给一个对应类型的常 ...

  9. Valera and Tubes

    C. Valera and Tubes time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. Mac与Window之间的共享文件

    Mac访问Window: Finder 菜单 “前往” ,然后“连接服务器”,在服务器地址输入 smb://windows主机名或ip地址/共享名(前提window已设置共享文件) Windows访问 ...