android實現(xiàn)下拉菜單三級聯(lián)動
android中的下拉菜單聯(lián)動應(yīng)用非常普遍,android中的下拉菜單用Spinner就能實現(xiàn),以下列子通過簡單的代碼實現(xiàn)三級菜單聯(lián)動。
一 樣式文件
<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)動邏輯代碼
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
* 三級聯(lián)動主要是靈活的應(yīng)用三維數(shù)組
*/
public class MainActivity extends Activity {
private String province[] = new String[]{"江西","湖南"};
private Spinner spinner1,spinner2,spinner3;
private int provinceindex;
private String city [][] = {{"南昌","贛州"},{"長沙","湘潭"}};
private String counstryside [][][] = {{{"青山湖區(qū)","南昌縣"},{"章貢區(qū)","贛縣"}},{{"長沙縣","沙縣"},{"湘潭縣","象限"}}};
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)時據(jù)為空的時候觸發(fā)的
}
});
}
}
三 結(jié)果

四 總結(jié)
三級聯(lián)動主要是靈活的應(yīng)用三維數(shù)組,這樣能很方便的通過數(shù)組索引將三個菜單關(guān)聯(lián),同時通過設(shè)置Spinner的setOnItemSelectedListener來監(jiān)聽選擇的動作,動態(tài)設(shè)置下拉菜單的內(nèi)容。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android SQLite數(shù)據(jù)庫的增 刪 查找操作
這篇文章主要介紹了Android SQLite數(shù)據(jù)庫的增 刪 查找操作,需要的朋友可以參考下2017-02-02
Android 7.0行為變更 FileUriExposedException解決方法
這篇文章主要介紹了Android 7.0行為變更 FileUriExposedException解決方法的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android使用post方式上傳圖片到服務(wù)器的方法
這篇文章主要介紹了Android使用post方式上傳圖片到服務(wù)器的方法,結(jié)合實例形式分析了Android文件傳輸?shù)南嚓P(guān)技巧,需要的朋友可以參考下2016-03-03
Android打賞功能實現(xiàn)代碼(支付寶轉(zhuǎn)賬)
這篇文章主要介紹了Android打賞功能之支付寶轉(zhuǎn)賬 ,需要的朋友可以參考下2017-12-12
Android 中ScrollView與ListView沖突問題的解決辦法
這篇文章主要介紹了Android 中ScrollView與ListView沖突問題的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握解決問題的辦法,需要的朋友可以參考下2017-10-10
flutter 動手?jǐn)]一個城市選擇citypicker功能
在一些項目開發(fā)中經(jīng)常會用到城市選擇器功能,今天小編動手?jǐn)]一個基于flutter 城市選擇citypicker功能,具體實現(xiàn)過程跟隨小編一起看看吧2021-08-08

