android常用組件之Spinner

來源:互聯網
上載者:User

標籤:

Spinner是下拉式清單方塊,使用者可以通過該控制項從所列item中進行選擇,還可以觸發item的監聽事件。

該執行個體通過ArrayAdapter類建立了含有30個item的下拉式清單,當使用者從所列的item中進行選擇後,會彈出一個訊息框,提示使用者所選條目。注意spinner條目監聽方法是setOnItemSelectedListener(),而不是setOnItemClickListener(),如果使用setOnItemClickListener()方法會出現秒退現象。

首先是布局檔案,添加一個Spinner組件:

<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="main.test_spinner.MainActivity" >    <Spinner        android:id="@+id/spinner1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:spinnerMode="dropdown" />   <!-- 通過設定spinnerMode可以設定item顯示方式,是下拉式(API為11以上)還是彈出對話方塊樣式的 --> </LinearLayout>

其次是strings.xml檔案,主要是label顯示,:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Test_Spinner</string></resources>

再次是android原始碼檔案:

package main.test_spinner;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.Toast;public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{    private Spinner spinner=null;    private ArrayAdapter<String> adapter=null;    @Override    protected void onCreate(Bundle savedInstanceState) {               super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                spinner=(Spinner) findViewById(R.id.spinner1);        adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1);//item顯示布局採用系統提供的                for(int i=0;i<30;i++)            adapter.add("第"+i+"項");        spinner.setAdapter(adapter);//關聯適配器        spinner.setSelection(2);//設定初始顯示項目,這裡是position two,因為位置從0開始計數        spinner.setOnItemSelectedListener(MainActivity.this);    }    @Override    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,            long arg3) {        // TODO Auto-generated method stub        Toast.makeText(MainActivity.this, "您選擇的是第"+arg2+"項!", Toast.LENGTH_LONG).show();    }    @Override    public void onNothingSelected(AdapterView<?> arg0) {        // TODO Auto-generated method stub            }}

最後是測試結果:

1、啟動時的結果:

2、點擊第五項結果顯示:

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.