andriod Spinner
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="下拉框1:" android:id="@+id/label"/>
<Spinner android:layout_height="wrap_content" android:layout_width="150dip" android:id="@+id/spinner1" android:drawSelectorOnTop="false"/>
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="下拉框2:" android:id="@+id/label"/>
<Spinner android:layout_height="wrap_content" android:layout_width="150dip" android:id="@+id/spinner2" android:drawSelectorOnTop="false"/>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/ok" android:id="@+id/ok"/>
</LinearLayout>
package com.example.yanlei.mytk; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { private Spinner spinner1;
private Spinner spinner2;
private Button ok;
private ArrayAdapter countiesAdapter;
private String[] mCounties = {"beijing", "guangdong", "guangxi", "hunan"};
private List<String> allCounties = new ArrayList<String>();
private String result = "你选择的是:"; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
ok = (Button) findViewById(R.id.ok); for (int i = 0; i < mCounties.length; i++) {
allCounties.add(mCounties[i]);
} countiesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, allCounties);
countiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(countiesAdapter); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.counties, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter); //单击第一个下拉按钮时,显示选择的值。
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View view,
int position, long id) {
// TODO Auto-generated method stub
String str = (String) spinner1.getAdapter().getItem((int) id);
setTitle(result + str);
} @Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub }
}); //单击第二个下拉按钮时,显示选择的值。
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View view,
int position, long id) {
String str = (String) spinner2.getAdapter().getItem(position);
setTitle(result + str);
} @Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}); //单击确定按钮,提取选择的值.
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setTitle(result + spinner1.getSelectedItem() + " - >> " + spinner2.getSelectedItem());
}
}); }
}
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="counties">
<item>AAA</item>
<item>BBB</item>
<item>CCC</item>
<item>DDD</item>
<item>EEE</item>
</string-array>
</resources>
andriod Spinner的更多相关文章
- Andriod 自定义控件之创建可以复用的组合控件
前面已学习了一种自定义控件的实现,是Andriod 自定义控件之音频条,还没学习的同学可以学习下,学习了的同学也要去温习下,一定要自己完全的掌握了,再继续学习,贪多嚼不烂可不是好的学习方法,我们争取学 ...
- Web Api 与 Andriod 接口对接开发经验
最近一直急着在负责弄Asp.Net Web Api 与 Andriod 接口开发的对接工作! 刚听说要用Asp.Net Web Api去跟 Andriod 那端做接口对接工作,自己也是第一次接触Web ...
- Android之Socket通信、List加载更多、Spinner下拉列表
Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户端向服务器发送请求后,服务 ...
- Spinner控件
首先在XML文件中声明一个Spinner控件: <Spinner android:id="@+id/spinnerId" android:layout_width=" ...
- Xamarin.Android之Spinner的简单探讨
一.前言 今天用了一下Spinner这个控件,主要是结合官网的例子来用的,不过官网的是把数据写在Strings.xml中的, 某种程度上,不是很符合我们需要的,比较多的应该都是从数据库读出来,绑定上去 ...
- Andriod小项目——在线音乐播放器
转载自: http://blog.csdn.net/sunkes/article/details/51189189 Andriod小项目——在线音乐播放器 Android在线音乐播放器 从大一开始就已 ...
- Spinner
资源的方式添加 安卓中的下拉框 Spinner可以通过xml string中配置 <string-array name="spingarr"> <item> ...
- Android--UI之Spinner
前言 最近一直在讲AndroidUI的开发,今天讲一下Spinner控件,这是一个列表选择框,可以弹出一个列表供用户选择.在本片博客中,会讲解Spinner的基本属性以及设置之后的效果,以及使用Sim ...
- Andriod学习笔记1:代码优化总结1
多行变一行 比如说开发一个简单的计算器应用程序,需要定义0-9的数字按钮,第一次就习惯性地写出了如下代码: Button btn0; Button btn1; Button btn2; Button ...
随机推荐
- windows环境下配置php和redis
Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. 一.Window 下安装 下载地址:https://github.com/dmajkic/redis/downl ...
- oracle 语句创建表空间、用户、授权
/*分为四步 */ /*第1步:创建临时表空间 */ create temporary tablespace yuhang_temp tempfile 'D:\oracledata\yuhang_te ...
- android xmlns:tools用法
一开始不明白,后来删掉这个属性之后发现会出现一个提示: pick preview layout from the "Fragment Layout" context menu 原来 ...
- Linux高级编程--09.线程互斥与同步
多个线程同时访问共享数据时可能会冲突,比如两个线程都要把某个全局变量增加1,这个操作在某平台需要三条指令完成: 从内存读变量值到寄存器 寄存器的值加1 将寄存器的值写回内存 假设两个线程在多处理器平台 ...
- JS算法总结
1.选择排序: var arr = [3,6,7,2,6,4,1,6,8,24,12,53]; function sort(arr){ // 当数组的长度小于1的时候结束递归 if(arr.lengt ...
- MongoDB入门一:安装与配置
引言 ——妈妈说名字太长排在后面或在标题堆儿中容易被大家发现. MongoDB的名字来源与单词humongous(极大的,巨大无比的)有关,它是一个可扩展.高性能.开源的NoSQL数据库. 之所以在g ...
- TypeWonder – 在任何网站上实时预览字体效果
TypeWonder 让网页字体的选择过程变得轻松愉快.它可以帮助您在任何网站上快速测试 Web 字体效果!输入网站网址,就能够即时预览的字体的实际效果,还可以从数百种字体中进行挑选,您还可以得到所需 ...
- Android 学习笔记之AndBase框架学习(二) 使用封装好的进度框,Toast框,弹出框,确认框...
PS:渐渐明白,在实验室呆三年都不如在企业呆一年... 学习内容: 1.使用AbActivity内部封装的方法实现进度框,Toast框,弹出框,确认框... AndBase中AbActivity封 ...
- Velocity魔法堂系列二:VTL语法详解
一.前言 Velocity作为历史悠久的模板引擎不单单可以替代JSP作为Java Web的服务端网页模板引擎,而且可以作为普通文本的模板引擎来增强服务端程序文本处理能力.而且Velocity被移植到不 ...
- 代码规范之争——[个人Week2作业]
这四个问题均是出自 http://goodmath.scientopia.org/2011/07/14/stuff-everyone-should-do-part-2-coding-standards ...