編輯:關於 Android 開發
以下以「省分—城市」的關聯選擇為例,示範如何實作具有連動效果的二級下拉列表。
package zye.client.Client;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class cityset extends Activity {
private String[] province = new String[] {"直轄市", "特別行政區", "黑龍江"};
private String[] city = new String[]{"北京", "上海", "天津", "重慶"};
private String[][] pandc = new String[][]{{"北京", "上海", "天津", "重慶"}, {"香港", "澳門"}, {"哈爾濱", "齊齊哈爾", "牡丹江", "大慶", "伊春", "雙鴨山", "鶴崗", "雞西", "佳木斯", "七台河", "黑河", "綏化", "大興安嶺"}};
private Spinner sp;
private Spinner sp2;
private Context context;
ArrayAdapter<String> adapter;
ArrayAdapter<String> adapter2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cityset);
context = this;
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, province);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp = (Spinner) findViewById(R.id.province);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(selectListener);
adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, city);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2 = (Spinner) findViewById(R.id.city);
sp2.setAdapter(adapter2);
}
private OnItemSelectedListener selectListener = new OnItemSelectedListener(){
public void onItemSelected(AdapterView parent, View v, int position, long id){
int pos = sp.getSelectedItemPosition();
adapter2 = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, pandc[pos]);
sp2.setAdapter(adapter2);
}
public void onNothingSelected(AdapterView arg0){
}
};
}
關鍵在於:為第一個 Spinner 設定 OnItemSelectedListener,當使用者選擇某個省分時,透過 sp.getSelectedItemPosition() 取得索引,再從二維陣列 pandc 中取出對應的城市清單,重新設定給第二個 Spinner 的 adapter,即可達到連動效果。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<Spinner android:id="@+id/province"
android:layout_width="fill_parent"
android:layout_height="45px"
android:layout_marginLeft="75px"
android:layout_marginRight="20px"
android:layout_marginTop="7px"
android:drawSelectorOnTop="true"
android:layout_alignParentRight="true" />
<Spinner android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="45px"
android:layout_marginLeft="75px"
android:layout_marginRight="20px"
android:layout_marginTop="3px"
android:drawSelectorOnTop="true"
android:layout_alignParentRight="true"
android:layout_below="@id/province" />
</RelativeLayout>
Android 開發學習之路:RxAndroid 與 Lambda 表達式
Android 開發學習之路:RxAndroid 與 Lambda 表達式 在 Android Studio 中使用 Lambda,需要透過 Retrolambda 等工
MSM8909 與 Android 5.1.1 鍵盤驅動分析:I2C、Input 子系統與掃描碼
概述 本文以 MSM8909 平台與 Android 5.1.1 為例,分析鍵盤驅動的基本實現方式。系統採用 SN7326 鍵盤擴展晶片,透過 I2C 介面讀取狀態暫存器
用 Eclipse 閱讀 Android 源碼:配置文件準備與常見報錯處理
想深入研究 Android 系統,直接讀源碼是最有效的途徑。雖然 Android Studio 已經是主流,但 Eclipse 在瀏覽龐大源碼樹時依然有其便利之處。本文記
解決 TabLayout 的 InvocationTargetException
解決 TabLayout 的 InvocationTargetException使用 android.support.design.widget.TabLayout 時,