您好,登錄后才能下訂單哦!
這篇文章主要介紹Android如何實現聯動下拉框二級地市聯動下拉框功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
日常使用軟件中,為了方便且規范輸入,會使用到下拉框進行輸入,如注冊時生日選項,購物時的地址輸入,都會用到下拉框,今日筆者為了鞏固已學的知識,實現了二級聯動下拉框用作回顧及分享給求知的新手。
思路/步驟:
在實現聯動下拉框之前,我們先對用到的ArrayAdapter和數據的封裝作必要的了解,Android 中提供了很多適配器的實現類,其中ArrayAdapter就其中之一。它可以通過泛型來指定要適配的數據類型,然后在構造函數中把要適配的數據傳入。如:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( getApplicationContext(), R.array.province, android.R.layout.simple_spinner_item);
ArrayAdapter中比較常用的泛型是String,但此處用了CharSequence(字符序列),通過指定泛型來裝配適合的數據類型。
這段代碼中有三個參數:
第一個參數Context是上下文,就是當前的Activity。
第二個參數是數據來源,R.array.province是存儲在xml文件中的數據。
第三個參數是Android SDK中內置的一個TextView。(此xml文件中只有一個TextView)。
通過創建一個ArrayAdapter處理存儲在xml中的省份地市數據,用Spinner控件處理ArrayAdapter處理好的數據,用TextView將數據顯示出來形成一個list供用戶點擊選擇。
主要實現代碼: 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;//聲明兩個下拉框 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);//創建資源 spinner1.setAdapter(adapter);//裝配數據 spinner1.setOnItemSelectedListener(new provinceClickListen());//監聽事件 spinner2.setOnItemSelectedListener(new CityClickListen());//監聽事件 } 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);//獲取當前選中項 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.def, R.layout.item);//初始化 Province = spinner1.getSelectedItem().toString();//當前選中的省份 /** 根據省份,裝配地市數據**/ 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("陜西")) { 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); } spinner2.setAdapter(adapter);//裝配地市數據 } public void onNothingSelected(AdapterView<?> parent) { } } class CityClickListen implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View v, int i, long l) { City = spinner2.getSelectedItem().toString();//當前選中的地市 if (City.equals("-城市-")) {//當前還沒選擇地市 } 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"/>
實現效果如下:
以上是“Android如何實現聯動下拉框二級地市聯動下拉框功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。