android實(shí)現(xiàn)下拉菜單三級(jí)聯(lián)動(dòng)
android中的下拉菜單聯(lián)動(dòng)應(yīng)用非常普遍,android中的下拉菜單用Spinner就能實(shí)現(xiàn),以下列子通過(guò)簡(jiǎn)單的代碼實(shí)現(xiàn)三級(jí)菜單聯(lián)動(dòng)。
一 樣式文件
<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>
二 聯(lián)動(dòng)邏輯代碼
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 * 三級(jí)聯(lián)動(dòng)主要是靈活的應(yīng)用三維數(shù)組 */ public class MainActivity extends Activity { private String province[] = new String[]{"江西","湖南"}; private Spinner spinner1,spinner2,spinner3; private int provinceindex; private String city [][] = {{"南昌","贛州"},{"長(zhǎng)沙","湘潭"}}; private String counstryside [][][] = {{{"青山湖區(qū)","南昌縣"},{"章貢區(qū)","贛縣"}},{{"長(zhǎng)沙縣","沙縣"},{"湘潭縣","象限"}}}; 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 //當(dāng)時(shí)據(jù)為空的時(shí)候觸發(fā)的 } }); } }
三 結(jié)果
四 總結(jié)
三級(jí)聯(lián)動(dòng)主要是靈活的應(yīng)用三維數(shù)組,這樣能很方便的通過(guò)數(shù)組索引將三個(gè)菜單關(guān)聯(lián),同時(shí)通過(guò)設(shè)置Spinner的setOnItemSelectedListener來(lái)監(jiān)聽(tīng)選擇的動(dòng)作,動(dòng)態(tài)設(shè)置下拉菜單的內(nèi)容。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中代碼運(yùn)行指定Apk的簡(jiǎn)單方法
這篇文章主要介紹了Android中代碼運(yùn)行指定Apk的簡(jiǎn)單方法,有需要的朋友可以參考一下2014-01-01Android Studio多渠道打包、自定義打包APK名稱
Android Studio為我們提供了簡(jiǎn)便的方法,可以多渠道打包,一次打包所有的渠道包。這篇文章主要介紹了Android Studio多渠道打包、自定義打包APK名稱,需要的朋友可以參考下2018-01-01Android在WebView中調(diào)用系統(tǒng)下載的方法
這篇文章主要為大家詳細(xì)介紹了Android在WebView中調(diào)用系統(tǒng)下載的簡(jiǎn)單使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android Filterable實(shí)現(xiàn)Recyclerview篩選功能的示例代碼
這篇文章主要介紹了Android Filterable實(shí)現(xiàn)Recyclerview篩選功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02基于標(biāo)準(zhǔn)http實(shí)現(xiàn)Android多文件上傳
這篇文章主要介紹了基于標(biāo)準(zhǔn)http實(shí)現(xiàn)Android多文件上傳的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01基于GridView和ActivityGroup實(shí)現(xiàn)的TAB分頁(yè)(附源碼)
今天為大家介紹下使用GridView和ActivityGroup實(shí)現(xiàn)的分頁(yè),這里需要將Activity轉(zhuǎn)換成Window,然后再換成成View添加到容器中,具體實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈2013-06-06Android Studio 代理配置指南(小結(jié))
這篇文章主要介紹了Android Studio 代理配置指南(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01