android实现下拉菜单 * 联动
作者:AirMario 时间:2022-10-13 15:46:53
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
标签:android,下拉菜单, , 联动
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java分形绘制山脉模型
2023-05-10 00:51:29
![](https://img.aspxhome.com/file/2023/1/80901_0s.jpg)
Java 自定义注解的魅力
2023-06-29 14:45:15
![](https://img.aspxhome.com/file/2023/6/65776_0s.png)
一文搞懂Java ScheduledExecutorService的使用
2022-11-22 14:23:35
C#中Winform窗体Form的关闭按钮变灰色的方法
2023-06-06 09:11:34
解决BigDecimal转long丢失精度的问题
2022-07-16 13:44:22
![](https://img.aspxhome.com/file/2023/2/83582_0s.jpg)
详解java.lang.reflect.Modifier.isInterface()方法
2023-07-27 18:25:25
MyBatis-Plus联表查询(Mybatis-Plus-Join)的功能实现
2023-11-25 03:24:33
![](https://img.aspxhome.com/file/2023/3/60023_0s.png)
Java中list.contains()的用法及拓展
2022-11-06 21:04:59
![](https://img.aspxhome.com/file/2023/9/113199_0s.jpg)
javac final变量未赋值检测案例讲解
2023-09-29 04:25:17
![](https://img.aspxhome.com/file/2023/7/66627_0s.jpg)
android PopupWindow 和 Activity弹出窗口实现方式
2021-07-14 16:27:36
![](https://img.aspxhome.com/file/2023/8/137498_0s.png)
详解java动态代理模式
2023-03-29 13:39:29
![](https://img.aspxhome.com/file/2023/0/71870_0s.png)
Java Spring框架简介与Spring IOC详解
2021-08-06 03:05:56
![](https://img.aspxhome.com/file/2023/6/96056_0s.png)
C++ 反汇编之关于Switch语句的优化措施
2023-09-10 15:00:15
![](https://img.aspxhome.com/file/2023/5/99285_0s.png)
Android定时器Timer的停止和重启实现代码
2022-10-03 23:25:43
Diycode开源项目实例搭建上拉加载和下拉刷新的Fragment
2023-06-17 01:13:03
![](https://img.aspxhome.com/file/2023/4/138094_0s.gif)
Java selenium处理极验滑动验证码示例
2023-12-19 19:10:09
![](https://img.aspxhome.com/file/2023/2/74892_0s.png)
Unity实现新手引导镂空效果
2022-07-04 22:50:23
![](https://img.aspxhome.com/file/2023/9/125999_0s.jpg)
C#用NPOI导出导入Excel帮助类
2022-09-27 00:23:43
Java Spring Security认证与授权及注销和权限控制篇综合解析
2021-07-04 08:14:17
![](https://img.aspxhome.com/file/2023/7/92537_0s.png)
Android ListView实现上拉加载下拉刷新和滑动删除功能
2021-09-24 19:08:04
![](https://img.aspxhome.com/file/2023/3/137573_0s.gif)