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 SQLite數(shù)據(jù)庫(kù)的增 刪 查找操作
這篇文章主要介紹了Android SQLite數(shù)據(jù)庫(kù)的增 刪 查找操作,需要的朋友可以參考下2017-02-02
Android 7.0行為變更 FileUriExposedException解決方法
這篇文章主要介紹了Android 7.0行為變更 FileUriExposedException解決方法的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android使用post方式上傳圖片到服務(wù)器的方法
這篇文章主要介紹了Android使用post方式上傳圖片到服務(wù)器的方法,結(jié)合實(shí)例形式分析了Android文件傳輸?shù)南嚓P(guān)技巧,需要的朋友可以參考下2016-03-03
Android 判斷是否能真正上網(wǎng)的實(shí)例詳解
這篇文章主要介紹了Android 判斷是否能真正上網(wǎng)的實(shí)例詳解相關(guān)資料,希望通過(guò)本文大家能夠掌握判斷是否上網(wǎng)的方法,需要的朋友可以參考下2017-10-10
Android打賞功能實(shí)現(xiàn)代碼(支付寶轉(zhuǎn)賬)
這篇文章主要介紹了Android打賞功能之支付寶轉(zhuǎn)賬 ,需要的朋友可以參考下2017-12-12
Android 中ScrollView與ListView沖突問(wèn)題的解決辦法
這篇文章主要介紹了Android 中ScrollView與ListView沖突問(wèn)題的解決辦法的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家掌握解決問(wèn)題的辦法,需要的朋友可以參考下2017-10-10
android studio的Handler簡(jiǎn)單實(shí)例代碼
今天通過(guò)實(shí)例代碼給大家介紹android studio的Handler簡(jiǎn)單用法,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-10-10
flutter 動(dòng)手?jǐn)]一個(gè)城市選擇citypicker功能
在一些項(xiàng)目開(kāi)發(fā)中經(jīng)常會(huì)用到城市選擇器功能,今天小編動(dòng)手?jǐn)]一個(gè)基于flutter 城市選擇citypicker功能,具體實(shí)現(xiàn)過(guò)程跟隨小編一起看看吧2021-08-08

