android 之 spinner的简单使用
先看spinner的效果图:


代码:
MainActivity
package com.mecury.spinnertest; import java.util.ArrayList; import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner; public class MainActivity extends ActionBarActivity { private Spinner spinnerButton;
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerButton = (Spinner) findViewById(R.id.spinnerButton);
spinner = (Spinner) findViewById(R.id.spinner2); /*静态的显示下来出来的菜单选项,显示的数组元素提前已经设置好了
* 第二个参数:已经编写好的数组
* 第三个数据:默认的样式
*/
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.number_array, android.R.layout.simple_spinner_item);
//设置spinner中每个条目的样式,同样是引用android提供的布局文件
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerButton.setAdapter(adapter);
spinnerButton.setPrompt("测试");
spinnerButton.setOnItemSelectedListener(new spinnerListener()); /*
* 动态添显示下来菜单的选项,可以动态添加元素
*/
ArrayList<String> list = new ArrayList<String>();
list.add("1.苹果");
list.add("2.橘子");
/*
* 第二个参数是显示的布局
* 第三个参数是在布局显示的位置id
* 第四个参数是将要显示的数据
*/
ArrayAdapter adapter2 = new ArrayAdapter(this, R.layout.item, R.id.textview,list);
spinner.setAdapter(adapter2);
spinner.setOnItemSelectedListener(new spinner2Listener());
} class spinnerListener implements android.widget.AdapterView.OnItemSelectedListener{ @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//将选择的元素显示出来
String selected = parent.getItemAtPosition(position).toString();
} @Override
public void onNothingSelected(AdapterView<?> parent) {
System.out.println("nothingSelect");
}
}
class spinner2Listener implements android.widget.AdapterView.OnItemSelectedListener{ @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String selected = parent.getItemAtPosition(position).toString();
} @Override
public void onNothingSelected(AdapterView<?> parent) {
System.out.println("nothingSelect");
}
}
}
main_activity的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context="com.mecury.spinnertest.MainActivity" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是静态的:"/>
<Spinner
android:id="@+id/spinnerButton"
android:layout_width="match_parent"
android:layout_height="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是动态的:"/>
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> </LinearLayout>
因为第一种是静态的实现方法,我们需要事先设置好spinner要显示的内容。在String.xml文件中添加需要显示的内容:
<string-array name="number_array">
<item>1000</item>
<item>2000</item>
<item>3000</item>
<item>4000</item>
<item>5000</item>
<item>6000</item>
<item>7000</item>
<item>8000</item>
<item>9000</item>
</string-array>
第二种是静态的实现方法,我们使用自己的显示布局item.xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ffff"
android:padding="10dp"
android:orientation="vertical" > <TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"/>
</LinearLayout>
android 之 spinner的简单使用的更多相关文章
- Xamarin.Android之Spinner的简单探讨
一.前言 今天用了一下Spinner这个控件,主要是结合官网的例子来用的,不过官网的是把数据写在Strings.xml中的, 某种程度上,不是很符合我们需要的,比较多的应该都是从数据库读出来,绑定上去 ...
- Android基本控件Spinner的简单使用【转】
Android基本控件Spinner的简单使用 感谢大佬:https://blog.csdn.net/bingocoder/article/details/80469939 学习过了Textview, ...
- 在android的spinner中,实现取VALUE值和TEXT值。 ZT
在android的spinner中,实现取VALUE值和TEXT值. 为了实现在android的 spinner实现取VALUE值和TEXT值,我尝试过好些办法,在网上查的资料,都是说修改适配器, ...
- 在android的spinner中,实现取VALUE值和TEXT值
为了实现在android的spinner实现取VALUE值和TEXT值,我尝试过好些办法,在网上查的资料,都是说修改适配器,刚开始我也是通过修改适配器的方法来做的,但是如果一个activity有多个s ...
- [置顶]
xamarin android自定义spinner
以前弄的一个下拉框时自带的spinner,感觉好丑,实际效果实在满足不了基本的UI界面要求,还是自己动手丰衣足食,看了网上关于android中自定义spinner的文章,感觉实现原理还是比较简单,所以 ...
- 我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍
GitHub Android Libraries Top 100 简介 本文转载于:https://github.com/Freelander/Android_Data/blob/master/And ...
- Xamarin.Android之封装个简单的网络请求类
一.前言 回忆到上篇 <Xamarin.Android再体验之简单的登录Demo> 做登录时,用的是GET的请求,还用的是同步, 于是现在将其简单的改写,做了个简单的封装,包含基于Http ...
- Spinner的简单学习
代码分析: package com.mecury.spinnertest; import java.util.ArrayList; import android.support.v7.app.Acti ...
- Android组件Spinner使用
Spinner组件是Android当中非常常用的一种用于下拉选择的组件. 本blog当中主要解决的几个问题: 如何在XML中初始化Spinner选项 如何使用代码的方式初始化Spinner选项 一个A ...
随机推荐
- 关于VR技术和未来发展---转
原文地址:http://mp.weixin.qq.com/s?__biz=MzA4MTIwNTczMQ==&mid=2651345594&idx=3&sn=2741ab7321 ...
- 【转载】C/C++ 函数指针 总结
转载自:http://blog.csdn.net/shihui512/article/details/9787125 什么是函数指针函数指针的声明函数指针的赋值函数指针的使用将函数作为其他函数的参数在 ...
- Linux - 文本格式转换
文本文档格式查看 Linux下的文档格式查看方法 cat -A <filename> | grep "^M$", 如果存在^M$字符就是Dos格式. Windows下的 ...
- Android学习笔记之DocumentBuilder的使用....
PS:当你的才华还撑不起你的野心时,那你需要静下心来学习..... 学习内容: 1.从服务器上获取XML文档... 2.解析XML文档中的内容... XML文件想必大家都非常的熟悉,可扩展的标记语 ...
- .net core 1.0 Web MVC 自定义认证过程
通过官方的介绍可知,若要本地开始部署搭建一个基于.net core 1.0的Web应用,需要下载dotnet SDK,或在Visual Studio IDE之上安装相关插件以布置开发环境.为了使开发环 ...
- 【第二课】深入理解Handler
简要讲解Handler是做什么的 我们知道,在Android中,app启动会启动一个进程一个线程——UI线程,UI线程是主线程,并且不允许这个线程阻塞超过5秒,一旦超过5秒就会ANR. 所以较为耗时的 ...
- 团队项目SCRUM项目6.0 7.0
6.0----------------------------------------------------- sprint演示 1.坚持所有的sprint都结束于演示. 团队的成果得到认可,会感觉 ...
- 三分 --- ZOJ 3203 Light Bulb
Light Bulb Problem's Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3203 Mean: ...
- (C#)WinForm窗体间传值
1.通过构造函数 特点:传值是单向的(不可以互相传值),实现简单 实现代码如下: 在窗体Form2中 int value1; string value2; public Form2 ( int v ...
- C#简单文件下载-3行代码
使用WebClient string url = "http://www.mozilla.org/images/feature-back-cnet.png"; WebClient ...