欢迎来到入门教程网!

Android

当前位置:主页 > 软件编程 > Android >

android实现下拉菜单三级联动

来源:本站原创|时间:2020-01-10|栏目:Android|点击:

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
  //当时据为空的时候触发的
  }
 });
 
 
 }
 
 
}

三 结果

四 总结

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

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

上一篇:Android自定义酒店日期选择器

栏    目:Android

下一篇:Android实现左上角(其他边角)倾斜的标签(环绕效果)效果

本文标题:android实现下拉菜单三级联动

本文地址:https://www.xiuzhanwang.com/a1/Android/9140.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有