1. 程式人生 > >使用Spinner實現省市三級聯動

使用Spinner實現省市三級聯動

       首先看一下xml檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="refresh.idothing.com.spinnerdemo.MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="horizontal"
    android:padding="10dp"
    android:weightSum="3">

    <Spinner android:id="@+id/spinner1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" ></Spinner>
    <Spinner android:id="@+id/spinner2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" ></Spinner>
    <Spinner android:id="@+id/spinner3"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" ></Spinner>
</LinearLayout>
         實現:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Spinner spinner1;
    private Spinner spinner2;
    private Spinner spinner3;
    private ArrayAdapter arrayAdapter1;
    private ArrayAdapter arrayAdapter2;
    private ArrayAdapter arrayAdapter3;
    private int position1 = 0;
    private int position2 = 0;

    //省級選項值
    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);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        spinner3 = (Spinner) findViewById(R.id.spinner3);
        setSupportActionBar(toolbar);
        arrayAdapter1 = new ArrayAdapter(this, R.layout.item_view, R.id.text, province);
        arrayAdapter2 = new ArrayAdapter(this, R.layout.item_view, R.id.text, city[0]);
        arrayAdapter3 = new ArrayAdapter(this, R.layout.item_view, R.id.text, county[0][0]);
        spinner1.setAdapter(arrayAdapter1);
        spinner1.setSelection(0);
        spinner2.setAdapter(arrayAdapter2);
        spinner2.setSelection(0);
        spinner3.setAdapter(arrayAdapter3);
        spinner3.setSelection(0);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                position1 = i;
                arrayAdapter2 = new ArrayAdapter(MainActivity.this, R.layout.item_view, R.id.text, city[position1]);
                spinner2.setAdapter(arrayAdapter2);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if(position1 > 2 && i < 3) {
                    position2 = i;
                }else if(position1 > 2 && i > 2){
                    position2 = 3;
                }
                arrayAdapter3 = new ArrayAdapter(MainActivity.this,R.layout.item_view,R.id.text,county[position1][position2]);
                spinner3.setAdapter(arrayAdapter3);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_wx:
                Toast.makeText(MainActivity.this,"微信",Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_wb:
                Toast.makeText(MainActivity.this,"微博",Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_mm:
                Toast.makeText(MainActivity.this,"陌陌",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

}