这里我使用3层级联菜单
这里就给示例把,不做过多的解释,代码中基本的注释
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="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.spinner.MainActivity" >
<Spinner
android:layout_weight="1"
android:id="@+id/spinner_1"
android:layout_height="30dp"
android:layout_width="wrap_content"/>
<Spinner
android:id="@+id/spinner_2"
android:layout_weight="1"
android:layout_height="30dp"
android:layout_width="wrap_content"/>
<Spinner
android:id="@+id/spinner_3"
android:layout_weight="1"
android:layout_height="30dp"
android:layout_width="wrap_content"/>
</LinearLayout>
MainActivity.java代码
package com.example.spinner;
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 spinner_1;//1
private Spinner spinner_2;//2
private Spinner spinner_3;//3
private ArrayAdapter<String> adapter1,adapter2,adapter3;//对应的适配器
private int provinceNum = 0;
//下拉单数据的初始化
private String province[] = {"江西省","上海市","江苏"};
private String city[][] = {{"南昌","赣州"},{"上海1","上海2"},{"江苏","南京"}};
private String area[][][] = {{{"南昌市","红谷滩","新建区"},{"章贡区","赣县"}},{{"上海11","上海111"},
{"上海22"}},{{"江苏11","江苏22"},{"南京11","南京22"}}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner_1 = (Spinner)findViewById(R.id.spinner_1);
spinner_2 = (Spinner)findViewById(R.id.spinner_2);
spinner_3 = (Spinner)findViewById(R.id.spinner_3);
//初始化,这里使用ArrayAdapter
adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, province);
adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, city[0]);
adapter3 = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, area[0][0]);
//设置选择事件
spinner_1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
adapter2 = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line, city[position]);
spinner_2.setAdapter(adapter2);
//保存1级菜单position,为3级菜单显示的准备
provinceNum = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
//2级菜单的选择事件
spinner_2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
adapter3 = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line,area[provinceNum][position]);
spinner_3.setAdapter(adapter3);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
spinner_1.setAdapter(adapter1);
spinner_2.setAdapter(adapter2);
spinner_3.setAdapter(adapter3);
}
}
结果图: