码迷,mamicode.com
首页 > 移动开发 > 详细

安卓下拉菜单(Spinner)的实现

时间:2014-12-31 18:27:44      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:spinner

一、先送上效果图:

技术分享

二、xml

<RelativeLayout 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: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:background="@drawable/blue"
    android:id="@+id/main"
    android:padding="0dp"
    >
    <Spinner 
        android:id="@+id/Spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/Color"
        />
</RelativeLayout>
三、代码实现:

package com.example.spinnerdemo;

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.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private Spinner spinner;
	private RelativeLayout relativeLayout;
	private ArrayAdapter<String> adapter;
	private int[] resource = {R.drawable.blue,R.drawable.red,R.drawable.green};
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		
		relativeLayout = (RelativeLayout) findViewById(R.id.main);
	
		spinner = (Spinner)this.findViewById(R.id.Spinner);
		spinner.setPrompt("设置背景颜色"); 
	
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,getData());
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
	
		spinner.setAdapter(adapter);  
		spinner.setSelection(0,true);//直接跳转到一个指定的数据
		spinner.setOnItemSelectedListener(new SelectedListener());
	}
	/**
	 * 数据源
	 * @return
	 */
	private List<String> getData() {
		List<String> items = new ArrayList<String>();
		items.add("blue");
		items.add("red");
		items.add("green");
		return items;
	}
	/**
	 * 下拉选项点击事件
	 * @author Administrator
	 *
	 */
	final class SelectedListener implements OnItemSelectedListener{

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			List<String> list = getData();
			relativeLayout.setBackgroundResource(resource[arg2]);
			toast(list.get(arg2));
		}
		/**
         * 当视图中的处于选中状态的条目全部消失时执行的回调函数。
         * 启动触控功能或适配器为空都可能导致选中条目消失。
         */
		public void onNothingSelected(AdapterView<?> arg0) {
			
		}
	}
	/**
	 * 自定义toast
	 * @param msg
	 */
	public void toast(String msg){
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
	//菜单
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}






安卓下拉菜单(Spinner)的实现

标签:spinner

原文地址:http://blog.csdn.net/u010885095/article/details/42297505

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!