android实现下拉菜单 * 联动
作者:AirMario 发布时间:2022-10-13 15:46:53
标签: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来监听选择的动作,动态设置下拉菜单的内容。
来源:https://blog.csdn.net/AirMario/article/details/53583993


猜你喜欢
- 前言相信有很多小伙伴,在日常的开发中都有遇到过需要调用第三方接口的需求吧,但是自己有没有写过接口提供给第三方使用呢,常规的都是我们调用别人的
- SpringBoot 1.5.9 版本加入actuator依赖后,访问/beans 等敏感的信息时候报错,如下Tue Mar 07 21:1
- 错误展示:Information:java: Errors occurred while compiling module 'emp
- 题意Description相信大家都做过"A+B Problem"了吧,这道题是它的加强版。输入两个整数 A , B ,
- 创建maven父子工程时遇到一个问题,当子工程的名称前缀和父工程的名称一样时,子工程会出现一系列的问题。比如我的父工程名称是microser
- ViewPager是一个常用的Android组件,不过通常我们使用ViewPager的时候不能实现左右无限循环滑动,在滑到边界的时候会看到一
- 在有些需求中会遇到,当鼠标滑过某个UI物体上方时,为了提醒用户该物体是可以交互时,我们需要添加一个动效和提示音。这样可以提高产品的体验感。解
- Android安全加密专题文章索引Android安全加密:对称加密Android安全加密:非对称加密Android安全加密:消息摘要Mess
- 一、负载均衡负载均衡(Load Balance): 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞
- 基础环境SpringBoot、Maven代码实现1.添加依赖<!--二维码生成 --><dependency&
- Android 列表组件 ListView列表组件是开发中经常用到组件,使用该组件在使用时需要为它提供适配器,由适配器提供来确定显示样式和显
- 通常在使用service更新应用时最常出现的问题就是Notification进度的更新问题、service在什么时间关闭以及需要我们自己在S
- 上下文:程序运行需要的环境(外部变量)上下文切换:将之前的程序需要的外部变量复制保存,然后切换到新的程序运行环境系统调用:(用户态陷入操作系
- 介绍了图的最小生成树的概念,然后介绍了求最小生成树的两种算法:Prim算法和Kruskal算法的原理,最后提供了基于邻接矩阵和邻接链表的图对
- 简介Security 是 Spring 家族中的一个安全管理框架。相比与另外一个安全框架Shiro,它提供了更丰富的功能,社区资源也比Shi
- 一、File流概念JAVA中针对文件的读写操作设置了一系列的流,其中主要有FileInputStream,FileOutputStream,
- 每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。代码public cla
- VB.NET中的除法运算符有两个:/(浮点除法)、\(整数除法)C#中的除法运算符只有一个:/(除法)VB.NET中的除法运算符与C#中的除
- 本文实例讲述了Android开发实现的标准体重计算器功能。分享给大家供大家参考,具体如下:运行结果界面: 界面设计<Rela
- 先看一段代码: private DataSet GetDataSet(string strsql){ string s