[置顶] 一步一步学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这个事件的,好了,今天就到这里了。
[置顶] 一步一步学android之事件篇——下拉列表事件的更多相关文章
- 一步一步学android之事件篇——单击事件
在使用软件的时候单击事件必不可少,比如我想确定.取消等都需要用户的单击,所有的单击事件都是由View.OnClickListener接口来进行处理的,接口定义如下: public static int ...
- 一步一步学android之事件篇——触摸事件
触摸事件顾名思义就是触摸手机屏幕触发的事件,当用户触摸添加了触摸事件的View时,就是执行OnTouch()方法进行处理,下面通过一个动态获取坐标的例子来学习OnTouchListener事件,效果如 ...
- [置顶] 新修改ADB,支持Android 4.2 系统 ,全部中文命令,手机屏幕截图等等
发过好几个ADB的工具,有很多朋友用了之后给我反馈了不少的意见和bug,这里非常感谢他们,所以今天花了一天的时间重新整理了一下ADB,并且修改了这些BUG.也有朋友建议我给一个修改列表,今天发这个帖子 ...
- [置顶] High Performance Canvas Game for Android
Rule #0 为移动平台进行优化 为移动平台进行优化是十分重要的,因为移动平台的性能大概只有桌面平台的1/10左右(*1),它通常意味着: 更慢的CPU速度,这意味着不经过优化的JavaScript ...
- [置顶] 和孩子们一起学Python编程
1. 推荐书名 Computer Programming for Kids and Other Beginners in Python, 4Ed.pdf 中文译名:<和孩子们一起学Pyt ...
- [置顶]【实用 .NET Core开发系列】- 导航篇
前言 此系列从出发点来看,是 上个系列的续篇, 上个系列因为后面工作的原因,后面几篇没有写完,后来.NET Core出来之后,注意力就转移到了.NET Core上,所以再也就没有继续下去,此是原因之一 ...
- 自定义置顶TOP按钮
简述一下,分为三个步骤: 1. 添加Html代码 2. 调整Css样式 3. 添加Jquery代码 具体代码如下: <style type="text/css"> #G ...
- 一步一步学android控件(之十五) —— DegitalClock & AnalogClock
原本计划DigitalClock和AnalogClock单独各一篇来写,但是想想,两个控件的作用都一样,就和在一起写一篇了. DegitalClock和AnalogClock控件主要用于显示当前时间信 ...
- 一步一步学android控件(之十六)—— CheckBox
根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式.今天主要学习如何自定义CheckBox样式.在CheckBox状态改变时有时需要做一些 ...
随机推荐
- Oracle多表的简单查询
Oracle多表的简单查询 .多表查询 多表查询是指基于两个和两个以上的表或是视图的查询. 问题:显示雇员名,雇员工资及所在部门的名字[笛卡尔集]? select t.ename,t.sal,t1.d ...
- hive regex insert join group cli
1.insert Insert时,from子句既能够放在select子句后,也能够放在insert子句前,以下两句是等价的 hive> FROM invites a INSERT OVERWRI ...
- VC环境下编译OpenSSL(仅仅是个示例,网上还有许多相关文章)
VC环境OpenSSL安装以及编程过程 SSL就是Secure Sockets Layer,是一种安全套接字协议,详情请参考链接中的介绍. 配置过程中需要生成一些mak文件,这些生成代码用perl脚本 ...
- golang各版本的变化
https://golang.org/doc/https://golang.org/doc/go1.6https://golang.org/doc/go1.5https://golang.org/do ...
- Android开发之导入错误
在导入Git库中更新下来的project的时候,自己手动的加入libs,assets等依赖库进去.可是导入project总是会莫名奇异的出现故障,特别是对Android系统库依赖的报错之类的. 解决方 ...
- Android 保存用户偏好设置
很多情况下都允许用户根据自己的习惯和爱好去设置软件,而我们需要保存这些设置,可以用一个专业保存用户偏好的类:SharedPreferences. 这个类是实现方法其实也就是创建和修改 XML 文件, ...
- Selenium WebDriver TestNg Maven Eclipse java 简单实例
环境准备 前提条件Eclipse 已经安装过 TestNg ,Maven 插件 新建一个普通的java项目 点击右键 configure->convert to Maven Project 之后 ...
- window2003远程桌面“已达最大连接数”
使用命令行强制注销远程登录用户 Fri, 04/19/2013 - 09:29 - admin 来源地址: http://space.itpub.net/10067101/viewspace-6147 ...
- 基于visual Studio2013解决面试题之0407数组差
题目
- input在苹果浏览器下变成圆角的解决方案
复制代码代码如下: .form-actions input{ ... -webkit-appearance: none; } 更新到iPhone一看,真爽,问题解决了.