上一篇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. QNX 线程 调度策略 优先级 时钟频率 同步

    /* * barrier1.c */ #include <stdio.h>#include <unistd.h>#include <stdlib.h>#includ ...

  2. vld(Visual Leak Detector) 内存泄露检测工具

    初识Visual Leak Detector 灵活自由是C/C++语言的一大特色,而这也为C/C++程序员出了一个难题.当程序越来越复 杂时,内存的管理也会变得越加复杂,稍有不慎就会出现内存问题.内存 ...

  3. 在纯C工程的main函数之前跑代码(手工找到程序入口点, 替换为我们自己的函数)

    在main函数之前跑代码的方法 方法: 手工找到程序入口点, 替换为我们自己的函数 写测试程序 // test.cpp : Defines the entry point for the consol ...

  4. Dropbox + Farbox高速创建免费博客小站

    创建自己的Dropbox账号(已有账号的略过) 注冊地址:Dropbox 点击链接注冊就好了,so easy: 账号注冊成功后,能够选择下载同步client(windows.Mac.ios.andro ...

  5. 10个优秀的 HTML5 &amp; CSS3 下拉菜单制作教程

    下拉菜单是一个非经常见的效果.在站点设计中被广泛使用.通过使用下拉菜单.设计者不仅能够在站点设计中营造出色的视觉吸引力,但也能够为站点提供了一个有效的导航方案.使用 HTML5 和 CSS3 能够更e ...

  6. HTTP实现长连接(TTP1.1和HTTP1.0相比较而言,最大的区别就是增加了持久连接支持Connection: keep-alive)

    HTTP实现长连接 HTTP是无状态的 也就是说,浏览器和服务器每进行一次HTTP操作,就建立一次连接,但任务结束就中断连接.如果客户端浏览器访问的某个HTML或其他类型的Web页中包含有其他的Web ...

  7. MVC3和MVC4中CRUD操作

    MVC3中EF实现的CRUD操作 public class HomeController : Controller { // // GET: /Home/ CarModelContainer db = ...

  8. SAP自带的创建报表工具

    SAP自带的工具有quickview和query两个主要的工具,当然还有其他的 quickview和query的区别主要是query支持系统之间的传输,quickview只能是用户的客户端创建使用,不 ...

  9. Net Core子应用由于配置引起IIS错误500.19

    Asp.Net Core子应用由于配置中重复添加模块会引起IIS错误500.19 ASP.NET Core已经从IIS中解耦,可以作为自宿主程序运行,不再依赖IIS. 但我们还是需要强大的IIS作为前 ...

  10. 让Android中的webview支持页面中的文件上传

    android webview在默认情况下是不支持网页中的文件上传功能的: 如果在网页中有<input type="file" />,在android webview中 ...