温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android中怎么实现下拉菜单三级联动

发布时间:2021-08-09 17:01:54 来源:亿速云 阅读:160 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关android中怎么实现下拉菜单三级联动,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

一 样式文件

<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中怎么实现下拉菜单三级联动有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI