首页 > 代码库 > 安卓下拉菜单(Spinner)的实现
安卓下拉菜单(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)的实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。