展会信息港展会大全

android 之spinner用法
来源:互联网   发布日期:2015-10-02 15:58:57   浏览:5280次  

导读:1.声明Spinner android:id=@+id/spinnerId android:layout_width=fill_parent android:layout_height=wrap_content / 2.声明数组zhangsanlisiwangwumaziyuexinchuxingxihangxiongfei 3.创建一个ArrayAdapter(给控件提供数据) (1)静态创建 Spinner spinne...

1.声明Spinner

android:id="@+id/spinnerId"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

2.声明数组zhangsanlisiwangwumaziyuexinchuxingxihangxiongfei

3.创建一个ArrayAdapter(给控件提供数据)

(1)静态创建

Spinner spinner = (Spinner) findViewById(R.id.spinnerId);

//创建adapter对象,参数1为上下文,参数2为数据的来源,

参数3为下拉菜单的样式(这里为系统自带的样式)

ArrayAdapter adapter =

ArrayAdapter.createFromResource(this,

R.array.students_array,

android.R.layout.simple_spinner_item);

//布局文件定义下拉菜单的选项的样子

adapter.setDropDownViewResource

(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

//下拉菜单的题目

spinner.setPrompt("测试");

(2)动态创建

List list = new ArrayList();

list.add("yuexin");

list.add("xihang");

list.add("xiongfei");

//参数1为上下文,参数2自定义的item,参数3为参数2布局中的textview(参数2中必须要有textview用于显示),参数4为准备显示的list

ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item, R.id.spinner_textView,list);

4.创建监听

spinner.setOnItemSelectedListener(new spinnerOnItemSelectedListener

());

class spinnerOnItemSelectedListener implements OnItemSelectedListener{

@Override

public void onItemSelected(AdapterView> adapterView,

View view, int position,

long id) {

//得到其中的值

String selected =

adapterView.getItemAtPosition(position).toString();

System.out.println(selected);

}

@Override

public void onNothingSelected(AdapterView> arg0) {

System.out.println("nothing");

}

}

代码:

MainActivity.java

package com.yx.spinner;

import java.util.ArrayList;

import java.util.List;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Spinner spinner = (Spinner) findViewById(R.id.spinnerId);

//动态加载、、、、、、、、、

List list = new ArrayList();

list.add("yuexin");

list.add("xihang");

list.add("xiongfei");

//参数1为上下文,参数2自定义的item,参数3为参数2布局中的textview(参数2中必须要有textview用于显示),参数4为准备显示的list

ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item, R.id.spinner_textView,list);

//动态加载、、、、

/*//静态加载,从strings.xml中加载

//创建adapter对象为spinner提供数据和样式,参数1为上下文,参数2为数据的来源(在strings.xml文件中定义的),参数3为下拉菜单的样式(这里为系统自带的样式)

ArrayAdapter adapter = ArrayAdapter.createFromResource(this,

R.array.students_array, android.R.layout.simple_spinner_item);

//布局文件定义下拉菜单的选项的样子

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

*/

spinner.setAdapter(adapter);

//下拉菜单的题目

spinner.setPrompt("测试");

spinner.setOnItemSelectedListener(new spinnerOnItemSelectedListener());

}

class spinnerOnItemSelectedListener implements OnItemSelectedListener{

@Override

public void onItemSelected(AdapterView> adapterView, View view, int position,

long id) {

//得到其中的值

String selected = adapterView.getItemAtPosition(position).toString();

System.out.println(selected);

}

@Override

public void onNothingSelected(AdapterView> arg0) {

System.out.println("nothing");

}

}

}

item.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@android:drawable/btn_dialog" />

android:id="@+id/spinner_textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

main.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

android:id="@+id/spinnerId"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港