1. 程式人生 > >Android實現省市區三級聯動效果Spinner

Android實現省市區三級聯動效果Spinner

public class MainActivity extends AppCompatActivity {
    Spinner s_sheng;
    ArrayAdapter<String> adapter_sheng;
    ArrayAdapter<String> adapter_shi;
    ArrayAdapter<String> adapter_xian;
    Spinner s_shi;
    Spinner s_xian;
    int sheng_postion;
    int shi_postion;
    String seletced_sheng;
    String seletced_shi;
    String seletced_xian;
    TextView t_v;
    String[] arr_sheng={"北京市","天津市","上海市","廣東省","河南省","重慶市","河北省","山西省","遼寧省","吉林省","黑龍江省","江蘇省","浙江省", "安徽省","福建省",
            "江西省","山東省","湖北省","湖南省","海南省","四川省","貴州省","雲南省","陝西省", "甘肅省","青海省","臺灣省"};
    String [][]arr_shi=new String[][] {
            {"北京市"},// 北京市
            {"天津市"},//天津市
            {"上海市"},//上海市
            {"廣州", "深圳", "韶關", "珠海", "汕頭", "佛山", "湛江", "肇慶", "江門", "茂名", "惠州", "梅州",
                    "汕尾", "河源", "陽江", "清遠", "東莞", "中山", "潮州", "揭陽", "雲浮"},//廣州市
            {"鄭州市","開封市","洛陽市","平頂山市","許昌市"

            },//河南省

    };
    String[][][]arr_xian= new String [][][]{
            {//北京市
                    {"東城區", "西城區", "崇文區", "宣武區", "朝陽區", "海淀區", "豐臺區", "石景山區", "門頭溝區",
                            "房山區", "通州區", "順義區", "大興區", "昌平區", "平谷區", "懷柔區", "密雲縣", "延慶縣"}
            },
            {//深圳
                    {"和平區", "河東區", "河西區", "南開區", "河北區", "紅橋區", "塘沽區", "漢沽區", "大港區", "東麗區"}
            },
            {//上海
                    {"長寧區", "靜安區", "普陀區", "閘北區", "虹口區"}
            },
            {//廣東
                    {"海珠區", "荔灣區", "越秀區", "白雲區", "蘿崗區", "天河區", "黃埔區", "花都區", "從化市", "增城市", "番禺區", "南沙區"}, //廣州
                    {"寶安區", "福田區", "龍崗區", "羅湖區", "南山區", "鹽田區"}, //深圳
                    {"武江區", "湞江區", "曲江區", "樂昌市", "南雄市", "始興縣", "仁化縣", "翁源縣", "新豐縣", "乳源縣"},//韶關
                    {"無"}
            },
            {//河南
                    {"中原區","二七區","管城區","金水區","上街區","惠濟區","鞏義市","滎陽市","新密市","新鄭市" ,"登封市","中牟縣" },
                    {"鼓樓區","龍亭區","順河區","禹王臺","金明區", "杞縣","通許縣", "尉氏縣", "開封縣" ,"蘭考縣"},
                    {"西工區","老城區","瀍河區","澗西區","吉利區","洛龍區","偃師市","孟津縣","新安縣","欒川縣","嵩縣","汝陽縣", "宜陽縣","洛寧縣","伊川縣"},
                    {"新華區","衛東區","湛河區","石龍區","舞鋼市","汝州市","寶豐縣","葉 縣","魯山縣","郟縣"},
                    {"魏都區","禹州市","長葛市","許昌縣","鄢陵縣", "襄城縣"}
            },



    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        t_v=(TextView) findViewById(R.id.t_v);
        s_sheng = (Spinner) findViewById(R.id.sheng);
        s_shi = (Spinner) findViewById(R.id.shi);
        s_xian = (Spinner) findViewById(R.id.xian);
        adapter_sheng = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arr_sheng);
        adapter_shi = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arr_shi[0]);
        adapter_xian = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arr_xian[0][0]);
        s_sheng.setAdapter(adapter_sheng);
        s_sheng.setSelection(0);
        s_shi.setAdapter(adapter_shi);
        s_shi.setSelection(0);
        s_xian.setAdapter(adapter_xian);
        s_xian.setSelection(0);
        s_sheng.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                sheng_postion=i;
                adapter_shi=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, arr_shi[i]);
                s_shi.setAdapter(adapter_shi);
                seletced_sheng=arr_sheng[i];
            }

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

            }
        });
        s_shi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                shi_postion=i;
                adapter_xian=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, arr_xian[sheng_postion][shi_postion]);
                s_xian.setAdapter(adapter_xian);
                seletced_shi=arr_shi[sheng_postion][i];

            }

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

            }
        });
        s_xian.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                seletced_xian=arr_xian[sheng_postion][shi_postion][i];
                t_v.setText(seletced_sheng+"-"+seletced_shi+"-"+seletced_xian);
            }

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

            }
        });
    }
}

設計思想:

1.所有的資料暫時儲存在數組裡,一次定義了3個數組分別為一維二維三位,來儲存省市縣鄉的資料 2.使用Spinner元件實現mvc三層的控制效果 3.包括ArrayAdapter建立以及Spinner設定裝配資料

三、成果演示


四、資源連結