您好,登錄后才能下訂單哦!
android中的下拉菜單聯(lián)動應用非常普遍,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)動主要是靈活的應用三維數(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 //當時據(jù)為空的時候觸發(fā)的 } }); } }
三 結果
四 總結
三級聯(lián)動主要是靈活的應用三維數(shù)組,這樣能很方便的通過數(shù)組索引將三個菜單關聯(lián),同時通過設置Spinner的setOnItemSelectedListener來監(jiān)聽選擇的動作,動態(tài)設置下拉菜單的內(nèi)容。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。