Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android Spinner 下拉框的靜態與動態兩種用法實戰

編輯:Android 開發實例

Spinner 相當於原生的下拉選擇框,位於 android.widget 包下,同時也是 View 的子類。它平時只展示當前選中項,點擊後才展開列表供用戶挑選,列表元素同樣來自 Adapter——這意味著只要你會用 ListView 的適配器,就會用 Spinner。

一、效果預覽

 

二、MainActivity 代碼

示例中放了兩個 Spinner:一個數據來自 resources 數組(靜態),一個數據來自代碼裡動態構建的 List(動態)。

 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);
        // 設置下拉後每個條目的樣式,同樣引用系統布局
        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.橘子");

        /* 參數依次為:上下文、條目布局、布局中的 TextView 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");
        }
    }
}

三、主布局 activity_main.xml

 <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>

四、靜態數據源:strings.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>

五、自定義條目布局 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>

六、幾個實用補充

setPrompt() 只在 Dialog 樣式(通過 android:spinnerMode="dialog")下才會顯示標題,默認的下拉彈窗樣式下它是無效的。

onItemSelected 在 Spinner 初次佈局完成時也會被觸發一次(position 為 0),如果只想在用戶真正操作時響應,需要加一個標誌位判斷,或者改用 setOnItemSelectedListener 之外的方案。這是新手最常困惑的一點。

onNothingSelected 只在沒有任何選項時才會回調,正常場景下幾乎用不到。

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