安卓四级级联下拉框效果实现

观书散遗帙,探古穷至妙。这篇文章主要讲述安卓四级级联下拉框效果实现相关的知识,希望能为你提供帮助。

突然想起来,安卓这块的随笔荒废好久了……T^T
    今天因为大创的任务的要求,需要实现四级级联的下拉框的效果,所以趁着这个机会利用spinner实现了级联下拉框的效果,以下是效果图。
【安卓四级级联下拉框效果实现】
安卓四级级联下拉框效果实现

文章图片

    话不多说,上代码了,首先是activity_main.xml,
安卓四级级联下拉框效果实现

文章图片
安卓四级级联下拉框效果实现

文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:layout_height="wrap_content" 6android:orientation="vertical" 7android:padding="8dp" > 8< Spinner 9android:id="@+id/spin_institue" 10android:layout_width="match_parent" 11android:layout_height="wrap_content" /> 12 13< Spinner 14android:id="@+id/spin_grade" 15android:layout_width="100dp" 16android:layout_height="wrap_content" /> 17 18< Spinner 19android:id="@+id/spin_prof" 20android:layout_width="match_parent" 21android:layout_height="wrap_content" /> 22< Spinner 23android:id="@+id/spin_class" 24android:layout_width="100dp" 25android:layout_height="wrap_content" /> 26< Button 27android:layout_width="match_parent" 28android:layout_height="wrap_content" 29android:text="上传" 30android:id="@+id/up_btn"/> 31 < /LinearLayout>

activity_main.xml    然后是对应的MainActivity.java
安卓四级级联下拉框效果实现

文章图片
安卓四级级联下拉框效果实现

文章图片
1 package com.example.tryy; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.view.View; 8 import android.widget.AdapterView; 9 import android.widget.ArrayAdapter; 10 import android.widget.Button; 11 import android.widget.Spinner; 12 import android.widget.Toast; 13 14 import java.util.HashMap; 15 16 public class MainActivity extends AppCompatActivity { 17private Spinner insspinner=null; 18private Spinner graspinner=null; 19private Spinner prospinner=null; 20private Spinner claspinner=null; 21private Button up_btn=null; 22ArrayAdapter< String> insAdapter = null; 23ArrayAdapter< String> graAdapter = null; 24ArrayAdapter< String> proAdapter = null; 25ArrayAdapter< String> claAdapter=null; 26static int insPosition = 0; 27static int graPosition=0; 28static int proPosition=0; 29static int claPosition=0; 30private String upload_str; 31HashMap< String, String> hm = new HashMap< String, String> (); 32 33 34 35private String[] ins = new String[] {"计算机科学与技术学院"}; 36private String[][] gra=new String[][]{ 37{"16","17","18","19"} 38}; 39private String[][][] prof=new String[][][]{ 40{{"计算机科学与技术","电子信息科学与技术","信息安全","网络工程"}, 41{"计算机科学与技术","电子信息科学与技术","信息安全"}, 42{"计算机科学与技术","电子信息科学与技术","信息安全","数据科学与大数据技术专业"}, 43{"计算机类"}} 44}; 45private String[][][][] cla=new String[][][][]{ 46{ 47{{"01","02","03","04","05","06","07"}, {"01","02","03","04"},{"01","02","03","04"},{"01"}}, 48{{"01","02","03","04","05","06"}, {"01","02","03","04"},{"01","02","03","04"}}, 49{{"01","02","03","04"}, {"01","02","03","04"},{"01","02","03","04"},{"01","02"}}, 50{{"01","02","03","04","05","06","07","08","09","10","11","12","13"}} 51} 52}; 53 54 55@Override 56protected void onCreate(Bundle savedInstanceState) { 57super.onCreate(savedInstanceState); 58setContentView(R.layout.activity_main); 59hm.put("计算机类", "0801"); 60hm.put("计算机科学与技术", "0810"); 61hm.put("电子信息科学与技术", "0820"); 62hm.put("网络工程", "0830"); 63hm.put("信息安全", "0840"); 64hm.put("数据科学与大数据技术专业", "0850"); 65up_btn=findViewById(R.id.up_btn); 66up_btn.setOnClickListener(new Buttonlistener()); 67setSpinner(); 68} 69private class Buttonlistener implements View.OnClickListener { 70public void onClick(View v) { 71switch (v.getId()) { 72case R.id.up_btn: 73//*************************************************************************** 74//这里写上传的代码,上传图片与识别码,识别码就是upload_str,它的构成为GGPPPPCC,GG年级,PPPP专业号,CC班级号,比如 17081004 代表17级计算机科学与技术4班 75 76Toast.makeText(MainActivity.this,upload_str,Toast.LENGTH_SHORT ).show(); 77break; 78default: 79break; 80} 81} 82} 83private void setSpinner(){ 84insspinner=findViewById(R.id.spin_institue); 85graspinner=findViewById(R.id.spin_grade); 86prospinner=findViewById(R.id.spin_prof); 87claspinner=findViewById(R.id.spin_class); 88 89insAdapter = new ArrayAdapter< String> (MainActivity.this, android.R.layout.simple_spinner_item, ins); 90insspinner.setAdapter(insAdapter); 91insspinner.setSelection(0,true); 92 93graAdapter = new ArrayAdapter< String> (MainActivity.this, android.R.layout.simple_spinner_item, gra[0]); 94graspinner.setAdapter(graAdapter); 95graspinner.setSelection(0,true); 96 97proAdapter = new ArrayAdapter< String> (MainActivity.this, android.R.layout.simple_spinner_item, prof[0][0]); 98prospinner.setAdapter(proAdapter); 99prospinner.setSelection(0,true); 100 101claAdapter = new ArrayAdapter< String> (MainActivity.this, android.R.layout.simple_spinner_item, cla[0][0][0]); 102claspinner.setAdapter(claAdapter); 103claspinner.setSelection(0,true); 104 105insspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 106{ 107 108// 表示选项被改变的时候触发此方法,主要实现办法:动态改变地级适配器的绑定值 109@Override 110public void onItemSelected(AdapterView< ?> arg0, View arg1, int position, long arg3) 111{ 112//position为当前省级选中的值的序号 113 114//将地级适配器的值改变为city[position]中的值 115graAdapter = new ArrayAdapter< String> ( 116MainActivity.this, android.R.layout.simple_spinner_item, gra[position]); 117// 设置二级下拉列表的选项内容适配器 118graspinner.setAdapter(graAdapter); 119insPosition = position; //记录当前省级序号,留给下面修改县级适配器时用 120} 121 122@Override 123public void onNothingSelected(AdapterView< ?> arg0) 124{ 125 126}}); 127 128 129//地级下拉监听 130graspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 131{ 132 133@Override 134public void onItemSelected(AdapterView< ?> arg0, View arg1, 135int position, long arg3) 136{ 137proAdapter = new ArrayAdapter< String> (MainActivity.this, 138android.R.layout.simple_spinner_item,prof[insPosition][position]); 139prospinner.setAdapter(proAdapter); 140graPosition=position; 141} 142 143@Override 144public void onNothingSelected(AdapterView< ?> arg0) 145{ 146 147}}); 148 149prospinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 150{ 151 152@Override 153public void onItemSelected(AdapterView< ?> arg0, View arg1, 154int position, long arg3) 155{ 156claAdapter = new ArrayAdapter< String> (MainActivity.this, 157android.R.layout.simple_spinner_item, cla[insPosition][graPosition][position]); 158claspinner.setAdapter(claAdapter); 159proPosition=position; 160 161} 162 163@Override 164public void onNothingSelected(AdapterView< ?> arg0) 165{ 166 167}}); 168 169claspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 170{ 171 172@Override 173public void onItemSelected(AdapterView< ?> arg0, View arg1, 174int position, long arg3) 175{ 176claPosition=position; 177String str=ins[insPosition]+gra[insPosition][graPosition]+prof[insPosition][graPosition][proPosition]+cla[insPosition][graPosition][proPosition][claPosition]; 178upload_str=gra[insPosition][graPosition]+hm.get(prof[insPosition][graPosition][proPosition])+cla[insPosition][graPosition][proPosition][claPosition]; 179Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT ).show(); 180} 181 182@Override 183public void onNothingSelected(AdapterView< ?> arg0) 184{ 185 186}}); 187} 188 189 }

MainActivity.java 

    推荐阅读