下拉式清單 spinner(android)

來源:互聯網
上載者:User

下拉式清單 spinner,這個使用很簡單,在 xml 中定義一個標籤,然後指定 ArrayAdapter<String> 就可以了,

Adapter 需要指定下拉的布局。關於下拉的資料,書中是寫用靜態數組,我這裡改成了從資源檔中讀取

java 代碼

package zziss.android.spinnerTest;

import java.util.Map;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class SpinnerTestActivity extends Activity {
    /** Called when the activity is first created. */
    
    private TextView iTv;
    private Spinner  iSpinner;
    private String[] items;
    private ArrayAdapter<Map<String,Integer>> iAda;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        iTv = (TextView)this.findViewById(R.id.tv);
        iSpinner = (Spinner)this.findViewById(R.id.spinner);
        Resources res = getResources();
        items = res.getStringArray(R.array.listItems);
        ArrayAdapter<String> ada = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,items);
        ada.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
        iSpinner.setAdapter(ada);
        iSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                iTv.setText(items[arg2]);
            }

            

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                
            }
            
        });
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />
    <Spinner 
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        />
</LinearLayout>

 

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, SpinnerTestActivity!</string>
    <string name="app_name">SpinnerTest</string>
    <string-array name="listItems" >
        <item >A型</item>
        <item >B型</item>
        <item >AB型</item>
        <item >O型</item>
    </string-array>

</resources>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.