安卓下拉菜单(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; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。