上一篇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. Codeforces Round #254 (Div. 2)D(预计)

    D. DZY Loves FFT time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. 服务确定撤销/删除/关闭 (ml81n)

    FUNCTION zrfc_mm006. *"---------------------------------------------------------------------- * ...

  3. shadow dom

    初识shadow dom 我们先看个input="range"的表现: what amazing ! 一个dom能表现出这么多样式嘛? 无论是初学者和老鸟都是不肯相信的,于是在好奇 ...

  4. perl uri_escape(urlencode ) 转换

    [root@wx03 lib]# cat a1.pl #引入模块 use URI::Escape; #urlencode $encoded = uri_escape("[中均]") ...

  5. Javascript 进阶 封装

    js中处处是对象,面向对象的第一步当然就是封装了,由于Js中没有类的概念,所以封装起来也比较麻烦,下面介绍两种js的封装. 1.使用约定优先的原则,将所有的私有变量以_开头 <script ty ...

  6. MFC界面相关(彩色工具栏)

    MFC工具栏控件 创建工具栏步骤: (1)在Resource View中插入新toolbar (2)在toolbar上双击增加按钮,更改ID为ID_BUTTON,编译后在resource.h中即可看到 ...

  7. linux pthread之学习篇

    在应用程序编程中,为了不影响与用户交互的性能,通常需要创建新的线程来处理一些比较耗时的. 不影响用户体验的工作.而这又通常分为两种情况: (1)需要临时创建一个线程来做某件特定的事,等事情做完时线程即 ...

  8. cp命令的实现

    #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #define BUFFERSIZE 4096 ...

  9. android图片压缩的3种方法实例

    android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...

  10. Android剪切板(ClipboardManager) 复制文本

    Android也有剪切板(ClipboardManager) 注意:导包的时候 API 11之前:  android.text.ClipboardManagerAPI 11之后:  android.c ...