Android開發之Spinner控制項使用

來源:互聯網
上載者:User

標籤:

      之前部落格中已經介紹了Spinner控制項的資料繫結,地址在:http://blog.csdn.net/zdw_wym/article/details/50000357,本篇將介紹Spinner的事件監聽器:OnItemSelectedListener

      Spinner組件的主要功能是用於進行下拉式清單的顯示,當使用者選中下拉式清單中的某個選項後可以使用Spinner類提供的setOnItemSelectedListener()方法進行監聽。

     在這之前讓我們首先瞭解一下OnItemSelectedListener介面:

 一、結構

public static interface AdapterView.OnItemSelectedListener

    二、概述

    定義了當選中視圖中的項目時調用的回呼函數的介面。

   三、公用方法

  public abstract void onItemSelected (AdapterView<?> parent, View view, int position, long id)

  當選中視圖中的項目時執行的回呼函數。 實現時如果需要訪問與選中條目關聯的資料,可以調 用 getItemAtPosition(position)。

  參數

  parent 發生選中事件的 AbsListView。

  view    AbsListView 中被選中的視圖。

  position    視圖在一覽中的位置(索引)。

  id 被點擊條目的行 ID。

 

  public abstract void onNothingSelected (AdapterView<?> parent)

  當視圖中的處於選中狀態的條目全部消失時執行的回呼函數。 啟動觸控功能或適配器為空白都可能導致選中條目消失。

  參數

       parent 沒有任何選中條目的 AdapterView。

      

  匿名內部類方式為Spinner設定OnItemSelectedListener監聽事件,代碼如下:

eduSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {     @Override     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {            //擷取到Spinner下列選項值            String val = eduSpinner.getItemAtPosition(position).toString();            Toast.makeText(getApplicationContext(), val, Toast.LENGTH_LONG).show();     }     @Override     public void onNothingSelected(AdapterView<?> parent) {     }});
樣本效果如所示:


Android開發之Spinner控制項使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.