关闭

二级联动菜单

112人阅读 评论(0) 收藏 举报

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 

          }

        }
    }

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5065次
    • 积分:327
    • 等级:
    • 排名:千里之外
    • 原创:29篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条