Android 之 下拉框(Spinner)的使用
来源:互联网 发布:php opcache 编辑:程序博客网 时间:2024/05/16 09:47
下拉列表 Spinner。
java代码
Spinner的使用,可以极大提高用户的体验性。当需要用户选择的时候,可以提供一个下拉列表将所有可选的项列出来。供用户选择。
使用XML作为数据源
1.新建一个android的工程
2.在values文件夹下新建一个arryas.xml文件:声明一个TextView控件和一个Spinner控件,代码如下:
spinner.xml
<?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:id="@+id/sptext" 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:entries="@array/books" /></LinearLayout>array.xml
<?xml version="1.0" encoding="UTF-8"?><resources> <string-array name="books"> <item>啊适度</item> <item>是</item> <item>度</item> <item>啊</item> <item>发给</item> <item>二十四</item> </string-array></resources>
java代码
package com.example.demo;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 spinner extends Activity{private TextView view;private Spinner spinner;private ArrayAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner); spinner = (Spinner) findViewById(R.id.spinner); view = (TextView) findViewById(R.id.sptext); //将可选的内容加入到ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.books, android.R.layout.simple_dropdown_item_1line); //设置下拉列表的风格 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //把ArrayAdapter添加到spinner中 spinner.setAdapter(adapter); //添加事件监视器 spinner.setOnItemSelectedListener(new OnItemSelectedListener(){@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stubview.setText("你使用什么手机:"+adapter.getItem(arg2));}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub} }); //设置默认值 spinner.setVisibility(view.VISIBLE); } }还有一种用数组保存数据
在转载地址http://imshare.iteye.com/blog/770950
0 0
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- 关于GridView的内容高度的问题
- linux中awk实例分享
- 老上海小时候去哪儿玩 旧时玩乐的好去处
- 给技术人上的管理课:控制和计划
- cocos2d:Android和iOS中Cocos2dx的横屏竖屏设置
- Android 之 下拉框(Spinner)的使用
- 基于SSL协议的双向认证 - SSL协议 [1]
- 【jQuery】事件- blur() 方法
- C# 类、结构与扩展方法
- crtmpserver 在VS2010下的build
- 心烦
- js的全局变量和局部变量
- iOS学习之UIDatePicker控件使用
- BDN FDN GSM11.11规范