二级联动菜单

activity_main.xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="选择" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <Spinner
            android:id="@+id/idTopSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:entries="@array/LoveProvice" />

        <Spinner
            android:id="@+id/idSubSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

values_provice.xml布局

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="LoveProvice">

        <item >北京</item>
        <item >上海</item>
        <item >广州</item>
    </string-array>
</resources>

MainActivity.java代码

package com.example.spinnertest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

       private Spinner spinnerTopProvince = null;
       private Spinner spinnerSubCitys = null;
       private String[][] citys ={{"朝阳" ,  "阳台" ,  "紫金" ,  "海淀"},
                                  {"抚顺" ,  "大连" ,  "青岛" ,  "烟台"},
                                  {"济南" ,  "菏泽" ,  "威海" ,  "单县" },
                                  {"开封" ,  "安阳" ,  "洛阳" ,  "南阳"}};
       private ArrayAdapter<CharSequence> arrayAdapter = null;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //得到控件
            spinnerTopProvince=(Spinner)super.findViewById(R.id.idTopSpinner);
            spinnerSubCitys=(Spinner)super.findViewById(R.id.idSubSpinner);

            //省份设置监听
            spinnerTopProvince.setOnItemSelectedListener(new myItemSelectedLisener());

        }

        private final class myItemSelectedLisener  implements OnItemSelectedListener
        {

          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
          {
              MainActivity.this.arrayAdapter = 
                   new ArrayAdapter<CharSequence>(MainActivity.this, 
                   android.R.layout.simple_spinner_item,MainActivity.this.citys[position]);
             spinnerSubCitys.setAdapter(arrayAdapter);
          }

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

          }

        }
    }

效果图
这里写图片描述
这里写图片描述

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

二级联动菜单

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭