您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關android中怎么實現下拉菜單三級聯動,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
一 樣式文件
<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>
二 聯動邏輯代碼
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 * 三級聯動主要是靈活的應用三維數組 */public class MainActivity extends Activity { private String province[] = new String[]{"江西","湖南"}; private Spinner spinner1,spinner2,spinner3; private int provinceindex; private String city [][] = {{"南昌","贛州"},{"長沙","湘潭"}}; private String counstryside [][][] = {{{"青山湖區","南昌縣"},{"章貢區","贛縣"}},{{"長沙縣","沙縣"},{"湘潭縣","象限"}}}; 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 //當時據為空的時候觸發的 } }); }}
看完上述內容,你們對android中怎么實現下拉菜單三級聯動有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。