Spinner的详解

在看完mars的Spinner之后觉得很一般,因为Spinner的用法精髓并没有展示出来

后来看了Android讲义 觉得讲得不错

一、静态配置

//layout

main.xml:

<Spinner 

       android:id="@+id/test"

       android:layout_width="fill_width"

       android:layout_height="wrap_content"

      android:entries="@array/books"

/>

//res中

arrays.xml

<resources>

     <string-array name="books">

  <item>tomtom</item>

  <item>tomtom</item>

  <item>tomtom</item>

      </string>

</resources>


二、动态配置

需要一个适配器

package org.crazyit.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class SpinnerTest2 extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		BaseAdapter ba = new BaseAdapter()
		{
			@Override
			public int getCount()
			{
				//指定一共包含10个选项
				return 10;
			}

			@Override
			public Object getItem(int position)
			{
				return null;
			}

			@Override
			public long getItemId(int position)
			{
				// TODO Auto-generated method stub
				return 0;
			}
//			//重写该方法,该方法返回的View将作为列表框的每项
//			@Override
//			public View getView(int position, View convertView, ViewGroup parent) 
//			{
//				TextView text = new TextView(SpinnerTest2.this);
//				text.setText(position"");
//				text.setTextSize(20);
//				text.setTextColor(R.color.red);
//				return text;
//			}
			//重写该方法,该方法返回的View将作为列表框的每项
			@Override
			public View getView(int position, View convertView, ViewGroup parent) 
			{
				//创建一个LinearLayout,并向其中添加2个组件
				LinearLayout line = new LinearLayout(SpinnerTest2.this);
				line.setOrientation(0);
				ImageView image = new ImageView(SpinnerTest2.this);
				image.setImageResource(R.drawable.icon);
				TextView text = new TextView(SpinnerTest2.this);
				text.setText(position + "");
				text.setTextSize(20);
				text.setTextColor(R.color.red);
				line.addView(image);
				line.addView(text);
				//返回LinearLayout实例
				return line;
			}		
		};
		Spinner spinner = (Spinner)findViewById(R.id.test);
		spinner.setAdapter(ba);
	}
}

我们大家都应该知道,android给我们提供了一个spinner控件,这个控件主要就是一个列表,那么我们就来说说这个控件吧,这个控件在以前的也看见过,但今天还是从新介绍一遍吧。Spinner位于 android.widget包下,每次只显示用户选中的元素,当用户再次点击时,会弹出选择列表供用户选择,而选择列表中的元素同样来自适配器。SpinnerView类得一个子类。讲完了这些,我们就给大家来看看是一个这么样的效果。

效果图:

字符串资源string.xml

Java代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Activity01!</string>
<string name="app_name">xh_spinner_test</string>
<string name="ys">您的爱好</string>
<string name="lq">篮球</string>
<string name="zp">足球</string>
<string name="pq">排球</string>
</resources>


颜色资源 colors.xml 

Java代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#fd8d8d</color>
<color name="green">#9cfda3</color>
<color name="blue">#8d9dfd</color>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
</resources>


布局文件 main.xml 

Java代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView 
android:text="@string/ys"
android:id="@+id/TextView01"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textSize="28dip"
/>
<Spinner
android:id="@+id/Spinner01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Java代码:
package eoe.sp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class Activity01 extends Activity {
final static int WRAP_CONETNT = -2;// 表示WRAP_CONTENT的常量
// 所有资源的图片(足球、篮球、排球) id的数组
int[] drawableIds = { R.drawable.football, R.drawable.basketball,
R.drawable.volleyball };
// 所有资源字符串 (足球、篮球、排球) id的数组
int[] msgIds = { R.string.zp, R.string.lq, R.string.pq };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner sp = (Spinner) findViewById(R.id.Spinner01);
BaseAdapter ba = new BaseAdapter() {

public int getCount() {
// 一共三个选项
return 3;
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
// 动态生成每个下拉项对应的View,每个下拉项View由LinearLayout
// 中包含一个ImageView及一个TextView构成
// 初始化LinearLayout
LinearLayout ll = new LinearLayout(Activity01.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
// 初始化ImageView
ImageView ii = new ImageView(Activity01.this);
ii.setImageDrawable((getResources().getDrawable(drawableIds[position])));
ll.addView(ii);
// 初始化TextView
TextView tv = new TextView(Activity01.this);
tv.setText(" " + getResources().getText(msgIds[position]));
tv.setTextColor(R.color.black);
tv.setTextSize(24);
ll.addView(tv);
return ll;
}
};
// 为Spinner设置内容适配器
sp.setAdapter(ba);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// 获取主界面TextView
TextView tv = (TextView) findViewById(R.id.TextView01);
// 获取当前选中选项对应的LinearLayout
LinearLayout ll = (LinearLayout) view;
// 获取其中的TextView
TextView tvn = (TextView) ll.getChildAt(1);
// 用StringBuilder动态生成信息
StringBuilder sb = new StringBuilder();
sb.append(getResources().getText(R.string.ys));
sb.append(":");
sb.append(tvn.getText());
// 信息设置进住界面
tv.setText(sb.toString());
}

public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值