欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现下拉菜单三级联动

程序员文章站 2022-06-29 18:30:35
android中的下拉菜单联动应用非常普遍,android中的下拉菜单用spinner就能实现,以下列子通过简单的代码实现三级菜单联动。 一 样式文件

android中的下拉菜单联动应用非常普遍,android中的下拉菜单用spinner就能实现,以下列子通过简单的代码实现三级菜单联动。

一 样式文件

<relativelayout 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: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_width="match_parent"
    android:layout_height="wrap_content"
    
    android:id="@+id/spn"
    android:dropdownwidth="200dp"/>
  
  <spinner android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/spn"
    android:id="@+id/city"
    android:dropdownwidth="200dp"/>
  <spinner android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/city"
    android:id="@+id/counstryside"
    android:dropdownwidth="200dp"/>
 
</relativelayout>

二 联动逻辑代码

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;
 
/**
 * @author zmc
 * 三级联动主要是灵活的应用三维数组
 */
public class mainactivity extends activity {
 private string province[] = new string[]{"江西","湖南"};
 private spinner spinner1,spinner2,spinner3;
 private int provinceindex;
 private string city [][] = {{"南昌","赣州"},{"长沙","湘潭"}};
 private string counstryside [][][] = {{{"青山湖区","南昌县"},{"章贡区","赣县"}},{{"长沙县","沙县"},{"湘潭县","象限"}}};
 arrayadapter<string> adapter1,adapter2,adapter3;
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 spinner1 = (spinner) findviewbyid(r.id.spn);
 adapter1 = new arrayadapter<>(this, android.r.layout.simple_dropdown_item_1line,province);
 spinner1.setadapter(adapter1);
 
 spinner2 = (spinner)findviewbyid(r.id.city);
 adapter2 = new arrayadapter<>(this, android.r.layout.simple_dropdown_item_1line,city[0]);
 spinner2.setadapter(adapter2);
 
 spinner3 = (spinner)findviewbyid(r.id.counstryside);
 adapter3 = new arrayadapter<>(this, android.r.layout.simple_dropdown_item_1line,counstryside[0][0]);
 spinner3.setadapter(adapter3);
 spinner1.setonitemselectedlistener(new onitemselectedlistener() {
 
  @override
  public void onitemselected(adapterview<?> parent, view view,
   int position, long id) {
  // todo auto-generated method stub
  provinceindex = position;
  adapter2 = new arrayadapter<>(mainactivity.this, android.r.layout.simple_dropdown_item_1line,city[position]);
  spinner2.setadapter(adapter2);
  }
 
  @override
  public void onnothingselected(adapterview<?> parent) {
  // todo auto-generated method stub
  
  }
 });
 
 spinner2.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,counstryside[provinceindex][position]);
  //adapter3.notifydatasetchanged();
  spinner3.setadapter(adapter3);
  }
 
  @override
  public void onnothingselected(adapterview<?> parent) {
  // todo auto-generated method stub
  //当时据为空的时候触发的
  }
 });
 
 
 }
 
 
}

三 结果

android实现下拉菜单三级联动

四 总结

三级联动主要是灵活的应用三维数组,这样能很方便的通过数组索引将三个菜单关联,同时通过设置spinner的setonitemselectedlistener来监听选择的动作,动态设置下拉菜单的内容。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。