小編給大家分享一下Android如何實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
古塔網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)公司于2013年開始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
針對(duì)AdapterView的拓展使用,Spinner實(shí)現(xiàn)省市區(qū)的三級(jí)聯(lián)動(dòng),具體內(nèi)容如下
其主要是通過使用Spinner的setOnItemSelectListener來實(shí)現(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;//省級(jí) private Spinner citySpinner;//市級(jí) private Spinner countySpinner;//區(qū)級(jí) private String[] province = new String[]{"北京","上海","天津","廣東"}; private String[][] city = new String[][]{ {"東城區(qū)","西城區(qū)","崇文區(qū)","宣武區(qū)","朝陽區(qū)","海淀區(qū)","豐臺(tái)區(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;//省級(jí)選中索引 private int cityPosition;//市級(jí)選中索引 @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); //對(duì)省級(jí)下拉實(shí)現(xiàn)監(jiān)聽,市級(jí)下拉需要根據(jù)不同的省級(jí)顯示不同內(nèi)容。 provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //需要在item選中省級(jí)的時(shí)候,動(dòng)態(tài)的改變市級(jí)對(duì)應(yīng)的顯示 cityAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,city[position]); //設(shè)置二級(jí)下拉列表當(dāng)中選項(xiàng)內(nèi)容適配器 citySpinner.setAdapter(cityAdapter); //記錄當(dāng)前的省級(jí)索引位置,留給下面修改縣級(jí)進(jìn)行適配使用 provicePosition = position; } @Override public void onNothingSelected(AdapterView<?> parent) { } }); //市級(jí)下拉監(jiān)聽 citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //需要在item選中市級(jí)的時(shí)候,動(dòng)態(tài)的改變縣級(jí)對(duì)應(yīng)的顯示 countyAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,county[provicePosition][position]);//首先確定是哪個(gè)省的。 //設(shè)置二級(jí)下拉列表當(dāng)中選項(xiàng)內(nèi)容適配器 countySpinner.setAdapter(countyAdapter); show.setText("選中的城市為:"+ province[provicePosition] + city[provicePosition][position]); cityPosition = position; } @Override public void onNothingSelected(AdapterView<?> parent) { } }); //區(qū)級(jí)的下拉 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) { } }); } }
運(yùn)行結(jié)果:
以上是“Android如何實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞標(biāo)題:Android如何實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
文章出自:http://muchs.cn/article36/iegjpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、企業(yè)建站、企業(yè)網(wǎng)站制作、、面包屑導(dǎo)航、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)