溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android如何實(shí)現(xiàn)聯(lián)動(dòng)下拉框二級(jí)地市聯(lián)動(dòng)下拉框功能

發(fā)布時(shí)間:2021-04-16 11:47:03 來(lái)源:億速云 閱讀:204 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

這篇文章主要介紹Android如何實(shí)現(xiàn)聯(lián)動(dòng)下拉框二級(jí)地市聯(lián)動(dòng)下拉框功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

日常使用軟件中,為了方便且規(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)類(lèi),其中ArrayAdapter就其中之一。它可以通過(guò)泛型來(lái)指定要適配的數(shù)據(jù)類(lèi)型,然后在構(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ù)類(lèi)型。

這段代碼中有三個(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)效果如下:

Android如何實(shí)現(xiàn)聯(lián)動(dòng)下拉框二級(jí)地市聯(lián)動(dòng)下拉框功能
Android如何實(shí)現(xiàn)聯(lián)動(dòng)下拉框二級(jí)地市聯(lián)動(dòng)下拉框功能
Android如何實(shí)現(xiàn)聯(lián)動(dòng)下拉框二級(jí)地市聯(lián)動(dòng)下拉框功能
Android如何實(shí)現(xiàn)聯(lián)動(dòng)下拉框二級(jí)地市聯(lián)動(dòng)下拉框功能
Android如何實(shí)現(xiàn)聯(lián)動(dòng)下拉框二級(jí)地市聯(lián)動(dòng)下拉框功能

以上是“Android如何實(shí)現(xiàn)聯(lián)動(dòng)下拉框二級(jí)地市聯(lián)動(dòng)下拉框功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI