android實(shí)現(xiàn)下拉菜單三級聯(lián)動
android中的下拉菜單聯(lián)動應(yīng)用非常普遍,android中的下拉菜單用Spinner就能實(shí)現(xiàn),以下列子通過簡單的代碼實(shí)現(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)時(shí)據(jù)為空的時(shí)候觸發(fā)的
}
});
}
}
三 結(jié)果
四 總結(jié)
三級聯(lián)動主要是靈活的應(yīng)用三維數(shù)組,這樣能很方便的通過數(shù)組索引將三個(gè)菜單關(guān)聯(lián),同時(shí)通過設(shè)置Spinner的setOnItemSelectedListener來監(jiān)聽選擇的動作,動態(tài)設(shè)置下拉菜單的內(nèi)容。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:Android
下一篇:Android實(shí)現(xiàn)左上角(其他邊角)傾斜的標(biāo)簽(環(huán)繞效果)效果
本文標(biāo)題:android實(shí)現(xiàn)下拉菜單三級聯(lián)動
本文地址:http://www.jygsgssxh.com/a1/Android/9140.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動登錄
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


