Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框二級(jí)地市聯(lián)動(dòng)下拉框功能
日常使用軟件中,為了方便且規(guī)范輸入,會(huì)使用到下拉框進(jìn)行輸入,如注冊(cè)時(shí)生日選項(xiàng),購(gòu)物時(shí)的地址輸入,都會(huì)用到下拉框,今日筆者為了鞏固已學(xué)的知識(shí),實(shí)現(xiàn)了二級(jí)聯(lián)動(dòng)下拉框用作回顧及分享給求知的新手。
思路/步驟:
在實(shí)現(xiàn)聯(lián)動(dòng)下拉框之前,我們先對(duì)用到的ArrayAdapter和數(shù)據(jù)的封裝作必要的了解,Android 中提供了很多適配器的實(shí)現(xiàn)類,其中ArrayAdapter就其中之一。它可以通過(guò)泛型來(lái)指定要適配的數(shù)據(jù)類型,然后在構(gòu)造函數(shù)中把要適配的數(shù)據(jù)傳入。如:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( getApplicationContext(), R.array.province, android.R.layout.simple_spinner_item);
ArrayAdapter中比較常用的泛型是String,但此處用了CharSequence(字符序列),通過(guò)指定泛型來(lái)裝配適合的數(shù)據(jù)類型。
這段代碼中有三個(gè)參數(shù):
第一個(gè)參數(shù)Context是上下文,就是當(dāng)前的Activity。
第二個(gè)參數(shù)是數(shù)據(jù)來(lái)源,R.array.province是存儲(chǔ)在xml文件中的數(shù)據(jù)。
第三個(gè)參數(shù)是Android SDK中內(nèi)置的一個(gè)TextView。(此xml文件中只有一個(gè)TextView)。
通過(guò)創(chuàng)建一個(gè)ArrayAdapter處理存儲(chǔ)在xml中的省份地市數(shù)據(jù),用Spinner控件處理ArrayAdapter處理好的數(shù)據(jù),用TextView將數(shù)據(jù)顯示出來(lái)形成一個(gè)list供用戶點(diǎn)擊選擇。
主要實(shí)現(xiàn)代碼: MainActivity.java
import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends Activity { Spinner spinner1, spinner2;//聲明兩個(gè)下拉框 String City, Province; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner1 = (Spinner) findViewById(R.id.spinner1);//綁定ID spinner2 = (Spinner) findViewById(R.id.spinner2);//綁定ID ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.province, android.R.layout.simple_spinner_item);//創(chuàng)建資源 spinner1.setAdapter(adapter);//裝配數(shù)據(jù) spinner1.setOnItemSelectedListener(new provinceClickListen());//監(jiān)聽(tīng)事件 spinner2.setOnItemSelectedListener(new CityClickListen());//監(jiān)聽(tīng)事件 } class provinceClickListen implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View v, int i, long l) { Spinner spinner = (Spinner) parent; String pro = (String) spinner.getItemAtPosition(i);//獲取當(dāng)前選中項(xiàng) ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.def, R.layout.item);//初始化 Province = spinner1.getSelectedItem().toString();//當(dāng)前選中的省份 /** 根據(jù)省份,裝配地市數(shù)據(jù)**/ if (pro.equals("北京")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.北京, R.layout.item); } else if (pro.equals("天津")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.天津, R.layout.item); } else if (pro.equals("河北")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.河北, R.layout.item); } else if (pro.equals("山西")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.山西, R.layout.item); } else if (pro.equals("內(nèi)蒙古")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.內(nèi)蒙古, R.layout.item); } else if (pro.equals("遼寧")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.遼寧, R.layout.item); } else if (pro.equals("吉林")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.吉林, R.layout.item); } else if (pro.equals("黑龍江")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.黑龍江, R.layout.item); } else if (pro.equals("上海")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.上海, R.layout.item); } else if (pro.equals("江蘇")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.江蘇, R.layout.item); } else if (pro.equals("浙江")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.浙江, R.layout.item); } else if (pro.equals("安徽")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.安徽, R.layout.item); } else if (pro.equals("福建")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.福建, R.layout.item); } else if (pro.equals("江西")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.江西, R.layout.item); } else if (pro.equals("山東")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.山東, R.layout.item); } else if (pro.equals("河南")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.河南, R.layout.item); } else if (pro.equals("湖北")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.湖北, R.layout.item); } else if (pro.equals("湖南")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.湖南, R.layout.item); } else if (pro.equals("廣東")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.廣東, R.layout.item); } else if (pro.equals("廣西")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.廣西, R.layout.item); } else if (pro.equals("海南")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.海南, R.layout.item); } else if (pro.equals("重慶")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.重慶, R.layout.item); } else if (pro.equals("四川")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.四川, R.layout.item); } else if (pro.equals("貴州")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.貴州, R.layout.item); } else if (pro.equals("云南")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.云南, R.layout.item); } else if (pro.equals("西藏")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.西藏, R.layout.item); } else if (pro.equals("陜西")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.陜西, R.layout.item); } else if (pro.equals("甘肅")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.甘肅, R.layout.item); } else if (pro.equals("青海")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.青海, R.layout.item); } else if (pro.equals("寧夏")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.寧夏, R.layout.item); } else if (pro.equals("新疆")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.新疆, R.layout.item); } else if (pro.equals("臺(tái)灣")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.臺(tái)灣, R.layout.item); } else if (pro.equals("香港")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.香港, R.layout.item); } else if (pro.equals("澳門(mén)")) { adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.澳門(mén), R.layout.item); } spinner2.setAdapter(adapter);//裝配地市數(shù)據(jù) } public void onNothingSelected(AdapterView<?> parent) { } } class CityClickListen implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View v, int i, long l) { City = spinner2.getSelectedItem().toString();//當(dāng)前選中的地市 if (City.equals("-城市-")) {//當(dāng)前還沒(méi)選擇地市 } else { Toast.makeText(getApplicationContext(), Province+"省"+City, Toast.LENGTH_LONG).show();//Toast顯示選中的省份城市 } } public void onNothingSelected(AdapterView<?> parent) {} } public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
此外還有布局文件: activity_main.xml
<LinearLayout 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:orientation="horizontal" 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=".MainActivity" > <TextView android:id="@+id/province" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="省份:" /> <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/city" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="城市:" /> <Spinner android:id="@+id/spinner2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/spinner_item" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="名稱" android:textColor="#000" android:textSize="16dp"/>
實(shí)現(xiàn)效果如下:
總結(jié)
以上所述是小編給大家介紹的Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框二級(jí)地市聯(lián)動(dòng)下拉框功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Flutter如何輕松實(shí)現(xiàn)動(dòng)態(tài)更新ListView淺析
在Android中通常都會(huì)用到listview.那么flutter里面怎么用呢?下面這篇文章主要給大家介紹了關(guān)于Flutter如何輕松實(shí)現(xiàn)動(dòng)態(tài)更新ListView的相關(guān)資料,需要的朋友可以參考下2022-02-02Android三種實(shí)現(xiàn)定時(shí)器的方法
本文給大家分享了3種Android實(shí)現(xiàn)定時(shí)器的方法的示例,,需要的朋友可以參考下2015-02-02獲取Android簽名證書(shū)的公鑰和私鑰的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇獲取Android簽名證書(shū)的公鑰和私鑰的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12Android解決getExternalStorageDirectory在29后廢棄問(wèn)題(推薦)
這篇文章主要介紹了Android解決getExternalStorageDirectory在29后廢棄問(wèn)題(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Android動(dòng)畫(huà)之雷達(dá)掃描效果
雷達(dá)掃描效果在我們?nèi)粘?huì)經(jīng)??吹?,比如在新浪微博上有一個(gè)雷達(dá)功能,感覺(jué)類似于微信附近的人。只是多了一個(gè)類似于雷達(dá)掃描效果的動(dòng)畫(huà),某些知名安全軟件也有這樣的雷達(dá)效果,因此今天在這里小編帶著大家學(xué)習(xí)一下。2016-08-08Android簡(jiǎn)單實(shí)現(xiàn)文件下載
這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)單實(shí)現(xiàn)文件下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Android Studio實(shí)現(xiàn)仿微信APP門(mén)戶界面詳解及源碼
這篇文章帶你通過(guò)Android studio來(lái)實(shí)現(xiàn)微信APP的門(mén)戶界面,主要說(shuō)明框架的各部分功能與實(shí)現(xiàn)過(guò)程,下文包含了整個(gè)開(kāi)發(fā)過(guò)程,以及解決問(wèn)題的思路并再末尾提供了源碼鏈接2021-10-10android studio與手機(jī)連接調(diào)試步驟詳解
這篇文章主要為大家詳細(xì)介紹了android studio與手機(jī)連接調(diào)試步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07