Android實現(xiàn)聯(lián)動下拉框二級地市聯(lián)動下拉框功能
日常使用軟件中,為了方便且規(guī)范輸入,會使用到下拉框進行輸入,如注冊時生日選項,購物時的地址輸入,都會用到下拉框,今日筆者為了鞏固已學的知識,實現(xiàn)了二級聯(lián)動下拉框用作回顧及分享給求知的新手。
思路/步驟:
在實現(xiàn)聯(lián)動下拉框之前,我們先對用到的ArrayAdapter和數(shù)據的封裝作必要的了解,Android 中提供了很多適配器的實現(xiàn)類,其中ArrayAdapter就其中之一。它可以通過泛型來指定要適配的數(shù)據類型,然后在構造函數(shù)中把要適配的數(shù)據傳入。如:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( getApplicationContext(), R.array.province, android.R.layout.simple_spinner_item);
ArrayAdapter中比較常用的泛型是String,但此處用了CharSequence(字符序列),通過指定泛型來裝配適合的數(shù)據類型。
這段代碼中有三個參數(shù):
第一個參數(shù)Context是上下文,就是當前的Activity。
第二個參數(shù)是數(shù)據來源,R.array.province是存儲在xml文件中的數(shù)據。
第三個參數(shù)是Android SDK中內置的一個TextView。(此xml文件中只有一個TextView)。
通過創(chuàng)建一個ArrayAdapter處理存儲在xml中的省份地市數(shù)據,用Spinner控件處理ArrayAdapter處理好的數(shù)據,用TextView將數(shù)據顯示出來形成一個list供用戶點擊選擇。
主要實現(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;//聲明兩個下拉框
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ù)據
spinner1.setOnItemSelectedListener(new provinceClickListen());//監(jiān)聽事件
spinner2.setOnItemSelectedListener(new CityClickListen());//監(jiān)聽事件
}
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();//當前選中的省份
/** 根據省份,裝配地市數(shù)據**/
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);//裝配地市數(shù)據
}
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"/>
實現(xiàn)效果如下:





總結
以上所述是小編給大家介紹的Android實現(xiàn)聯(lián)動下拉框二級地市聯(lián)動下拉框功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
Flutter如何輕松實現(xiàn)動態(tài)更新ListView淺析
在Android中通常都會用到listview.那么flutter里面怎么用呢?下面這篇文章主要給大家介紹了關于Flutter如何輕松實現(xiàn)動態(tài)更新ListView的相關資料,需要的朋友可以參考下2022-02-02
Android解決getExternalStorageDirectory在29后廢棄問題(推薦)
這篇文章主要介紹了Android解決getExternalStorageDirectory在29后廢棄問題(推薦),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
Android Studio實現(xiàn)仿微信APP門戶界面詳解及源碼
這篇文章帶你通過Android studio來實現(xiàn)微信APP的門戶界面,主要說明框架的各部分功能與實現(xiàn)過程,下文包含了整個開發(fā)過程,以及解決問題的思路并再末尾提供了源碼鏈接2021-10-10

