Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現三級聯動下拉框 下拉列表spinner的實例

Android實現三級聯動下拉框 下拉列表spinner的實例

編輯:關於Android編程

主要實現辦法:動態加載各級下拉值的適配器

在監聽本級下拉框,當本級下拉框的選中值改變時,隨之修改下級的適配器的綁定值

\ \ \

\ \ \

XML布局:


    
    
    

實現代碼:

package com.example.android.demo.spinner;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity
{
    private Spinner provinceSpinner = null;  //省級(省、直轄市)
    private Spinner citySpinner = null;     //地級市
    private Spinner countySpinner = null;    //縣級(區、縣、縣級市)
    ArrayAdapter provinceAdapter = null;  //省級適配器
    ArrayAdapter cityAdapter = null;    //地級適配器
    ArrayAdapter countyAdapter = null;    //縣級適配器
    static int provincePosition = 3;

    
    //省級選項值
    private String[] province = new String[] {"北京","上海","天津","廣東"};//,"重慶","黑龍江","江蘇","山東","浙江","香港","澳門"};
    //地級選項值
    private String[][] city = new String[][] 
            {
                    { "東城區", "西城區", "崇文區", "宣武區", "朝陽區", "海澱區", "豐台區", "石景山區", "門頭溝區",
                            "房山區", "通州區", "順義區", "大興區", "昌平區", "平谷區", "懷柔區", "密雲縣",
                            "延慶縣" },
                    { "長寧區", "靜安區", "普陀區", "閘北區", "虹口區" },
                    { "和平區", "河東區", "河西區", "南開區", "河北區", "紅橋區", "塘沽區", "漢沽區", "大港區",
                            "東麗區" },
                    { "廣州", "深圳", "韶關" // ,"珠海","汕頭","佛山","湛江","肇慶","江門","茂名","惠州","梅州",
                    // "汕尾","河源","陽江","清遠","東莞","中山","潮州","揭陽","雲浮"
                    }
            };

    //縣級選項值
    private String[][][] county = new String[][][] 
            {
                    {   //北京
                        {"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},
                        {"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"}
                    },
                    {    //上海
                        {"無"},{"無"},{"無"},{"無"},{"無"}
                    },
                    {    //天津
                        {"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"}
                    },
                    {    //廣東
                        {"海珠區","荔灣區","越秀區","白雲區","蘿崗區","天河區","黃埔區","花都區","從化市","增城市","番禺區","南沙區"}, //廣州
                        {"寶安區","福田區","龍崗區","羅湖區","南山區","鹽田區"}, //深圳
                        {"武江區","浈江區","曲江區","樂昌市","南雄市","始興縣","仁化縣","翁源縣","新豐縣","乳源縣"}  //韶關
                    }
            };

    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setSpinner();
    }
    
    /*
     * 設置下拉框
     */
    private void setSpinner()
    {        
        provinceSpinner = (Spinner)findViewById(R.id.spin_province);
        citySpinner = (Spinner)findViewById(R.id.spin_city);
        countySpinner = (Spinner)findViewById(R.id.spin_county);

        //綁定適配器和值
        provinceAdapter = new ArrayAdapter(MainActivity.this,
                android.R.layout.simple_spinner_item, province);
        provinceSpinner.setAdapter(provinceAdapter);
        provinceSpinner.setSelection(3,true);  //設置默認選中項,此處為默認選中第4個值

        cityAdapter = new ArrayAdapter(MainActivity.this, 
                android.R.layout.simple_spinner_item, city[3]);
        citySpinner.setAdapter(cityAdapter);
        citySpinner.setSelection(0,true);  //默認選中第0個

        countyAdapter = new ArrayAdapter(MainActivity.this,
                android.R.layout.simple_spinner_item, county[3][0]);
        countySpinner.setAdapter(countyAdapter);
        countySpinner.setSelection(0, true);

        
        //省級下拉框監聽
        provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            // 表示選項被改變的時候觸發此方法,主要實現辦法:動態改變地級適配器的綁定值
            @Override
            public void onItemSelected(AdapterView arg0, View arg1, int position, long arg3)
            {
                //position為當前省級選中的值的序號

                //將地級適配器的值改變為city[position]中的值
                cityAdapter = new ArrayAdapter(
                        MainActivity.this, android.R.layout.simple_spinner_item, city[position]);
                // 設置二級下拉列表的選項內容適配器
                citySpinner.setAdapter(cityAdapter);
                provincePosition = position;    //記錄當前省級序號,留給下面修改縣級適配器時用
            }
            @Override
            public void onNothingSelected(AdapterView arg0)
            {

            }

        });

        
        //地級下拉監聽
        citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView arg0, View arg1,
                    int position, long arg3)
            {
                countyAdapter = new ArrayAdapter(MainActivity.this,
                        android.R.layout.simple_spinner_item, county[provincePosition][position]);
                countySpinner.setAdapter(countyAdapter);
            }
            @Override
            public void onNothingSelected(AdapterView arg0)
            {

            }
        });
    }
}

要獲取下拉框spinner中選中的值,用下面這方法就OK了

provinceSpinner.getSelectedItem().toString();

citySpinner.getSelectedItem().toString();

countySpinner.getSelectedItem().toString();


THE END


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved