Android——列表選擇框(Spinner)

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   ar   os   使用   

通常情況下,如果列表選擇框中要顯示的清單項目是可知的,那麼可以將其儲存在數組資源檔中,然後通過數組資源來為列表選擇框指定清單項目。這樣就可以在不編寫Java代碼的情況下實現一個下拉選擇框。

1.在布局檔案中添加一個<spinner>標記,並為其指定android:entries屬性,具體代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" ><Spinner android:entries="@array/ctype"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/spinner"/></LinearLayout>

其中android:entries屬性是用來指定清單項目的,如果在布局檔案中不指定該屬性,可以在Java代碼中通過為其指定適配器的方式指定;

2.編寫用於指定清單項目的數組資源檔,並將其儲存在res\values目錄中,這裡將其命名為arrays.xml,在該檔案中添加一個字串數組,名稱為ctype,具體代碼如下

<?xml version="1.0" encoding="UTF-8"?><resources>    <string-array name="ctype">        <item>ID</item>        <item>Student Card</item>        <item>Army Card</item>        <item>Work Card</item>        <item>Other</item>    </string-array></resources>

在螢幕上添加列表選擇框後,可以使用列表選擇框的getSelectedItem()方法擷取列表選擇框的選中值,可以使用下面的代碼:

Spinner spinner=(Spinner)findViewById(R.id.spinner);spinner.getSelectedItem();

如果想要在使用者選擇不同的清單項目後,執行相應的處理,則可以為該列表選擇框添加OnItemSelectedListener事件監聽器。例如,為spinner添加挑選清單事件監聽器,並在onItemSelected()方法中擷取選擇項的值輸出到日誌中,可以使用如下代碼:

package com.basillee.blogdemo;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.Spinner;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Spinner spinner=(Spinner)findViewById(R.id.spinner);spinner.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View arg1,int pos, long id) {// TODO Auto-generated method stubString result=parent.getItemAtPosition(pos).toString();//擷取選擇項的值Log.i("spinner", result);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});}}

下面介紹通過指定適配器的方式指定列表的方式指定清單項目的方法。

(1)建立一個配接器物件,通常使用ArrayAdapter類。在Android中,建立適配器通常可以使用以下兩種方法:一種是通過數組資源檔建立;另一種是通過java裡面的字串數組建立。

  • 通過數組資源檔建立適配器,需要使用ArrayAdapter類的createFromResource()方法,具體代碼如下:
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.ctype,android.R.layout.simple_dropdown_item_1line);
  • 通過Java代碼建立如下
String[]ctype=new String[]{"ID","Student Card","Army Card"};     ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,ctype);

(2)為適配器設定下拉式清單的選項樣式:

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

(3)將適配器與挑選清單框關聯:

spinner.setAdapter(adapter);

 

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.