Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Spinner 下拉清單控件用法詳解(靜態與動態兩種綁定)

編輯:關於 Android 程式設計

Android 提供了琳瑯滿目的 UI 控件,其中 Spinner 就是一個相當實用的下拉式清單。它位於 android.widget 套件之下,一次只會顯示使用者當前選中的項目;當使用者再次點擊時,會彈出選擇清單供挑選,而清單裡的元素同樣來自於 Adapter(配接器)。作為 View 的子類,Spinner 幾乎是所有需要「多選一」場景的首選。

先看 Spinner 的實際效果:

以下用一個範例同時示範「靜態」與「動態」兩種綁定方式。

MainActivity

 package com.mecury.spinnertest; import java.util.ArrayList; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; public class MainActivity extends ActionBarActivity { private Spinner spinnerButton; private Spinner spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinnerButton = (Spinner) findViewById(R.id.spinnerButton); spinner = (Spinner) findViewById(R.id.spinner2); /*靜態的顯示下來出來的菜單選項,顯示的數組元素提前已經設置好了 * 第二個參數:已經編寫好的數組 * 第三個數據:默認的樣式 */ ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.number_array, android.R.layout.simple_spinner_item); //設置spinner中每個條目的樣式,同樣是引用android提供的布局文件 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerButton.setAdapter(adapter); spinnerButton.setPrompt("測試"); spinnerButton.setOnItemSelectedListener(new spinnerListener()); /* * 動態添顯示下來菜單的選項,可以動態添加元素 */ ArrayList<String> list = new ArrayList<String>(); list.add("1.蘋果"); list.add("2.橘子"); /* * 第二個參數是顯示的布局 * 第三個參數是在布局顯示的位置id * 第四個參數是將要顯示的數據 */ ArrayAdapter adapter2 = new ArrayAdapter(this, R.layout.item, R.id.textview,list); spinner.setAdapter(adapter2); spinner.setOnItemSelectedListener(new spinner2Listener()); } class spinnerListener implements android.widget.AdapterView.OnItemSelectedListener{ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //將選擇的元素顯示出來 String selected = parent.getItemAtPosition(position).toString(); } @Override public void onNothingSelected(AdapterView<?> parent) { System.out.println("nothingSelect"); } } class spinner2Listener implements android.widget.AdapterView.OnItemSelectedListener{ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String selected = parent.getItemAtPosition(position).toString(); } @Override public void onNothingSelected(AdapterView<?> parent) { System.out.println("nothingSelect"); } } }

main_activity 的版面配置:

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" tools:context="com.mecury.spinnertest.MainActivity" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="這是靜態的:"/> <Spinner android:id="@+id/spinnerButton" android:layout_width="match_parent" android:layout_height="30dp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="這是動態的:"/> <Spinner android:id="@+id/spinner2" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> 

第一個 Spinner 採用靜態方式,需要事先在 string.xml 裡把要顯示的內容定義好:

 <string-array name="number_array"> <item>1000</item> <item>2000</item> <item>3000</item> <item>4000</item> <item>5000</item> <item>6000</item> <item>7000</item> <item>8000</item> <item>9000</item> </string-array>

第二個 Spinner 則示範使用自訂的顯示版面 item.xml,讓每個條目的呈現更為彈性:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ffff" android:padding="10dp" android:orientation="vertical" > <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp"/> </LinearLayout>

以上就是 Android 應用開發中 Spinner 控件的基礎用法。掌握靜態與動態兩種資料綁定方式,你就能在各種表單與設定頁面中游刃有餘地使用下拉清單了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved