Android實現(xiàn)省市區(qū)三級聯(lián)動
針對AdapterView的拓展使用,Spinner實現(xiàn)省市區(qū)的三級聯(lián)動,具體內(nèi)容如下
其主要是通過使用Spinner的setOnItemSelectListener來實現(xiàn)。
代碼示例:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testspinnerprovince.MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/selection"
android:padding="6dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<Spinner
android:id="@+id/provinceSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Spinner
android:id="@+id/citySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Spinner
android:id="@+id/countySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="6dp"
android:layout_marginTop="80dp"
android:text="@string/selected"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView show;
private Spinner provinceSpinner;//省級
private Spinner citySpinner;//市級
private Spinner countySpinner;//區(qū)級
private String[] province = new String[]{"北京","上海","天津","廣東"};
private String[][] city = new String[][]{
{"東城區(qū)","西城區(qū)","崇文區(qū)","宣武區(qū)","朝陽區(qū)","海淀區(qū)","豐臺區(qū)","石景山區(qū)","門頭溝區(qū)",
"房山區(qū)","通州區(qū)","順義區(qū)","大興區(qū)","昌平區(qū)","平谷區(qū)","懷柔區(qū)","密云縣","延慶縣"},
{"長寧區(qū)","靜安區(qū)","普陀區(qū)","閘北區(qū)","虹口區(qū)"},
{"和平區(qū)","河?xùn)|區(qū)","河西區(qū)","南開區(qū)","河北區(qū)","紅橋區(qū)","塘沽區(qū)","漢沽區(qū)","大港區(qū)","東麗區(qū)"},
{"廣州","深圳","韶關(guān)"}};
private String[][][] county = new String[][][]{{
//北京
{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},
{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},},{
//上海
{"無"},{"無"},{"無"},{"無"},{"無"},},{
//天津
{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},},{
//廣東
{"海珠區(qū)","荔灣區(qū)","越秀區(qū)","白云區(qū)","蘿崗區(qū)","天河區(qū)","黃浦區(qū)","花都區(qū)","從化市","增城市"
,"番禺區(qū)","南沙區(qū)"},
{"寶安區(qū)","福田區(qū)","龍崗區(qū)","羅湖區(qū)","南山區(qū)","鹽田區(qū)"},
{"武江區(qū)","湞江區(qū)","曲江區(qū)","樂昌市","南雄市","始興縣","仁化縣","翁源縣","新豐縣","乳源縣"}
}
};
//Spinner想要填充肯定需要適配器
private ArrayAdapter<String> provinceAdapter;
private ArrayAdapter<String> cityAdapter;
private ArrayAdapter<String> countyAdapter;
private int provicePosition;//省級選中索引
private int cityPosition;//市級選中索引
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();//初始化
}
//控件初始化
private void initViews() {
provinceSpinner = (Spinner) findViewById(R.id.provinceSpinner);
citySpinner = (Spinner) findViewById(R.id.citySpinner);
countySpinner = (Spinner) findViewById(R.id.countySpinner);
show = (TextView) findViewById(R.id.show);
//初始化適配器及顯示的內(nèi)容
provinceAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,province);
provinceSpinner.setAdapter(provinceAdapter);
//對省級下拉實現(xiàn)監(jiān)聽,市級下拉需要根據(jù)不同的省級顯示不同內(nèi)容。
provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//需要在item選中省級的時候,動態(tài)的改變市級對應(yīng)的顯示
cityAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,city[position]);
//設(shè)置二級下拉列表當(dāng)中選項內(nèi)容適配器
citySpinner.setAdapter(cityAdapter);
//記錄當(dāng)前的省級索引位置,留給下面修改縣級進行適配使用
provicePosition = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//市級下拉監(jiān)聽
citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//需要在item選中市級的時候,動態(tài)的改變縣級對應(yīng)的顯示
countyAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,county[provicePosition][position]);//首先確定是哪個省的。
//設(shè)置二級下拉列表當(dāng)中選項內(nèi)容適配器
countySpinner.setAdapter(countyAdapter);
show.setText("選中的城市為:"+ province[provicePosition] + city[provicePosition][position]);
cityPosition = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//區(qū)級的下拉
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
show.setText("選中的城市為:"+ province[provicePosition] + city[provicePosition][cityPosition]
+ county[provicePosition][cityPosition][position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
運行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android PickerView實現(xiàn)三級聯(lián)動效果
- 最好用的Android省市區(qū)三級聯(lián)動選擇效果
- Android日期選擇器實現(xiàn)年月日三級聯(lián)動
- Android中使用開源框架Citypickerview實現(xiàn)省市區(qū)三級聯(lián)動選擇
- Android自定義WheelView地區(qū)選擇三級聯(lián)動
- Android省市區(qū)三級聯(lián)動控件使用方法實例講解
- android-wheel控件實現(xiàn)三級聯(lián)動效果
- Android使用android-wheel實現(xiàn)省市縣三級聯(lián)動
- Android實現(xiàn)聯(lián)動下拉框 下拉列表spinner的實例代碼
- Android實現(xiàn)城市選擇三級聯(lián)動
相關(guān)文章
基于Android實現(xiàn)的文件同步設(shè)計方案
隨著用戶對自身數(shù)據(jù)保護意識的加強,讓用戶自己維護自己的數(shù)據(jù)也成了獨立開發(fā)產(chǎn)品時的一個賣點,若只針對少量的文件進行同步,則實現(xiàn)起來比較簡單,當(dāng)針對一個多層級目錄同步時,情況就復(fù)雜多了,本文我分享下我的設(shè)計思路2023-10-10
Android手勢識別器GestureDetector使用詳解
這篇文章主要為大家詳細(xì)介紹了Android手勢識別器GestureDetector的使用方法解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Android AlertDialog自定義樣式實現(xiàn)代碼
這篇文章主要介紹了Android AlertDialog自定義樣式實現(xiàn)代碼的相關(guān)資料,這里提供了實例代碼,一個簡單示例,需要的朋友可以參考下2016-12-12
Kotlin Select協(xié)程多路復(fù)用的實現(xiàn)詳解
select是Kotlin 1.6中的特性,即選擇最快的結(jié)果。select與async、Channel結(jié)合使用,可以大大提高程序的響應(yīng)速度,還可以提高程序的靈活性、擴展性2022-09-09
Android Service(不和用戶交互應(yīng)用組件)案例分析
Service是在一段不定的時間運行在后臺,不和用戶交互應(yīng)用組件,本文將詳細(xì)介紹,需要了解的朋友可以參考下2012-12-12
使用TransitionDrawable實現(xiàn)多張圖片淡入淡出效果
這篇文章主要為大家詳細(xì)介紹了使用TransitionDrawable實現(xiàn)多張圖片淡入淡出效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08

