对于Spinner控件的介绍和使用方法,可以先看之前写过的一篇博客:Spinner控件详解
本篇就基于这个知识点完成一个简单的小案例:
根据介绍,先写一个布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请选择您喜欢的城市:" /> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:spinnerMode="dialog" android:entries="@array/city_labels" ><!-- 使用entries属性,传入的是values文件夹下的arrays.xml内的数据 --> </Spinner> <!--android:spinnerMode="dialog" 设置弹出的形式为对话框形式 --> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Spinner android:popupBackground="#9900ff00" android:id="@+id/spinnerCountry" android:layout_width="fill_parent" android:layout_height="wrap_content" ><!-- 通过适配器来加入属性值 --> <!-- 动态载入数据 android:popupBackground="#9900ff00"设置背景--> </Spinner> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Test</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string-array name="city_labels"> <item>北京 </item> <item>上海 </item> <item>广州 </item> <item>深圳 </item> </string-array> </resources>
package com.example.test; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private Spinner spinner, spinnerCountry; private TextView textView; private List<CharSequence> data = null; private ArrayAdapter<CharSequence> adapter; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 生命周期方法 super.setContentView(R.layout.activity_main); // 设置要使用的布局管理器 spinner = (Spinner) findViewById(R.id.spinner); textView = (TextView) findViewById(R.id.text); spinnerCountry = (Spinner) findViewById(R.id.spinnerCountry); spinnerCountry.setPrompt("");// 在列表中显示 data = new ArrayList<CharSequence>(); data.add("中國"); data.add("美國"); data.add("日本"); adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item, this.data);//定义下拉列表 //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerCountry.setAdapter(adapter); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String[] cities = getResources().getStringArray( R.array.city_labels);// 获取列表数据 textView.setText("您喜欢的城市是:" + cities[position]);// 显示 } public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); spinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //String[] countries = data.toArray(new String[data.size()]);// 获取列表数据 ,an array of the elements from this List. Toast.makeText(MainActivity.this, "您的国籍是:"+data.get(position), Toast.LENGTH_SHORT).show(); } public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); } }
模拟器下一个Spinner是一个绿色背景,不知道为何录屏之后是上边效果。。。