Android简易实战教程--第四十话《Spinner》
对于Spinner控件的介绍和使用方法,可以先看之前写过的一篇博客:Spinner控件详解
本篇就基于这个知识点完成一个简单的小案例:
根据介绍,先写一个布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请选择您喜欢的城市:" /> <Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:entries="@array/city_labels" ><!-- 使用entries属性,传入的是values文件夹下的arrays.xml内的数据 -->
</Spinner>
<!--android:spinnerMode="dialog" 设置弹出的形式为对话框形式 -->
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> <Spinner
android:popupBackground="#9900ff00"
android:id="@+id/spinnerCountry"
android:layout_width="fill_parent"
android:layout_height="wrap_content" ><!-- 通过适配器来加入属性值 -->
<!-- 动态载入数据 android:popupBackground="#9900ff00"设置背景-->
</Spinner> </LinearLayout>
而数据源在valuse下的String.xml里面写:
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">Test</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string> <string-array name="city_labels">
<item>北京 </item>
<item>上海 </item>
<item>广州 </item>
<item>深圳 </item>
</string-array>
</resources>
对应的Activity中的代码谢了出来:
package com.example.test; import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.graphics.Color;
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;
import android.widget.Toast; public class MainActivity extends Activity {
private Spinner spinner, spinnerCountry;
private TextView textView;
private List<CharSequence> data = null;
private ArrayAdapter<CharSequence> adapter; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.activity_main); // 设置要使用的布局管理器
spinner = (Spinner) findViewById(R.id.spinner);
textView = (TextView) findViewById(R.id.text);
spinnerCountry = (Spinner) findViewById(R.id.spinnerCountry); spinnerCountry.setPrompt("");// 在列表中显示
data = new ArrayList<CharSequence>();
data.add("中國");
data.add("美國");
data.add("日本");
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_dropdown_item, this.data);//定义下拉列表
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCountry.setAdapter(adapter); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String[] cities = getResources().getStringArray(
R.array.city_labels);// 获取列表数据
textView.setText("您喜欢的城市是:" + cities[position]);// 显示 } public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub }
}); spinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//String[] countries = data.toArray(new String[data.size()]);// 获取列表数据 ,an array of the elements from this List.
Toast.makeText(MainActivity.this, "您的国籍是:"+data.get(position), Toast.LENGTH_SHORT).show(); } public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub }
}); }
}
运行展示:
模拟器下一个Spinner是一个绿色背景,不知道为何录屏之后是上边效果。。。
Android简易实战教程--第四十话《Spinner》的更多相关文章
- Android简易实战教程--第五十话《动画扫描》
祝新年快乐!2017(一起)前行. 转载博客请注明出处:道龙的博客 本篇简答的小案例,使用动画知识,完成一个类似雷达扫描效果,并且加入自定义进度条.对于自定义进度条前面有很详细的解析和案例了,本篇就结 ...
- Android简易实战教程--第四十二话《Spinner下拉级联效果》
本篇承接第四十话第四十话<Spinner> 参考博客:http://blog.csdn.net/yayun0516 进入正题: Strings加入第一级数据: <string-arr ...
- Android简易实战教程--第四十六话《RecyclerView竖向和横向滚动》
Android5.X后,引入了RecyclerView,这个控件使用起来非常的方便,不但可以完成listView的效果,而且还可以实现ListView无法实现的效果.当然,在新能方便也做了大大的提高. ...
- Android简易实战教程--第四十八话《Android - Timer、TimerTask和Handler实现倒计时》
之前本专栏文章中的小案例有写到:第三十九话<Chronometer实现倒计时> 以及使用异步实现倒计时:第三十三话< AsyncTask异步倒计时> 本篇文章 结合Timer. ...
- Android简易实战教程--第四十九话《满屏拖动的控件》
今天做个有意思的效果吧,控件的拖拽,简单实用,逻辑清晰点3分钟看完. 说的很高大上,其实就是拖动Button按钮跟着鼠标位置满手机屏幕跑罢了. 直接上简单的代码吧: public class Main ...
- Android简易实战教程--第四十五话《几种对话框》
Android中提供了各种原生的对话框,在使用简单的功能的时候,还不比考虑自定义,使用原生的也能完成功能.本篇简单小案例就介绍三种对话框. 还是直接上代码吧: 布局中三个点击事件的按钮: <Li ...
- Android简易实战教程--第四十四话《ScrollView和HorizontalScrollView简单使用》
一.ScrollView 由于手机屏幕的高度有限,当普通布局放不下现实和的内容时,ScrollView视图(滚动视图)就会派上用场,因为数据可以往下滚动显示. 二.HorizontalScrollVi ...
- Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》
在之前的小案例中写过一篇使用HttpUrlConnection获取网络数据的例子.在OKhttp盛行的时代,当然要学会怎么使用它,本篇就对其基本使用做一个介绍,然后再使用它的接口回调的方式获取相同的数 ...
- Android简易实战教程--第四十三话《上拉加载与下拉刷新》
ListView的下拉刷新很常见,很多开源的框架都能做到这个效果,当然也可以自己去实现.本篇案例是基于xlistview的. 布局: <RelativeLayout xmlns:android= ...
随机推荐
- jQuery系列 第四章 jQuery框架的选择器
第四章 jQuery框架的选择器 4.1 jQuery选择器说明 jQuery 最核心的组成部分就是选择器引擎.它完全继承了 CSS 的风格,可以对 DOM 元 素的标签名.属性名.状态等进行快速准确 ...
- 更换yum源
1. 首先备份 sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2. 使用阿里云的 ...
- 创建类似于Oracle中SYS_GUID() 的方法
CREATE or REPLACE FUNCTION "sys_guid"()RETURNS "pg_catalog"."varchar" ...
- hermite矩阵
在读线代书.因为之前并没有上过线性代数的课.所以决定把基础打牢牢. 读书的时候当然会出现不懂的概念和术语或者定理什么的.所以在这记录一下啦--- hermit矩阵要理解它好像先要知道什么是共轭(con ...
- [LeetCode] Circular Array Loop 环形数组循环
You are given an array of positive and negative integers. If a number n at an index is positive, the ...
- 一步一步学习Vue(十三)
最近比较忙,忙着工作交接,忙着招人.忙着各种会,更新很少,这一篇也是作为本入门系列的最后一篇了,以后可能会写一些自己在前端这块的心得或者体会了,无关乎使用什么框架,可能就是原生的js.dom.编程模式 ...
- 将python代码打印成pdf
将python代码打印成pdf,打印出来很丑,完全不能看. mac下:pycharm 编辑器有print的功能,但是会提示: Error: No print service found. 所以需要一个 ...
- pytorch 移动端框架 thnets 附c示例代码
前年年前做一个手机移动端图像识别项目的时候, 先后尝试了mxnet,thnets,caffe,tensorflow. 当时的情况是,mxnet内存管理奇差,内存经常由于模型运算分配不足,app挂掉. ...
- Hello,World!!----来自半个新OIer的问候
为了记录OI生涯的点点滴滴故申请此博客,说是半个新OIer,其实我的OI生涯并不算很短了吧...现在也算是初三下学期了...也算是混迹NOIP第5个年头了.文化课上也没什么临考压力,那就好好准备第5场 ...
- hdu 5492 (暴力+nice)
题意:在矩阵中,找一条路从 (1,1)->(n,m),使方差最小 思路: T = (N+M−1)∑N+M−1i=1(Ai−Aavg)2 将N + M - 1乘进去,即求1 ~ N+M-1,(N ...