Android實現三級聯動下拉框 下拉選單spinner的例項程式碼
這篇文章介紹了Android實現三級聯動下拉框 下拉選單spinner的例項程式碼,有需要的朋友可以參考一下
主要實現辦法:動態載入各級下拉值的介面卡
在監聽本級下拉框,當本級下拉框的選中值改變時,隨之修改下級的介面卡的繫結值
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="wrap_content"
android:orientation="horizontal"
android:padding="8dp" >
<Spinner
android:id="@+id/spin_province"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spin_city"
android:layout_width="112dp"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spin_county"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
實現程式碼:
複製程式碼 程式碼如下:
package com.example.android.demo.spinner;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity
{
private Spinner provinceSpinner = null; //省級(省、直轄市)
private Spinner citySpinner = null; //地級市
private Spinner countySpinner = null; //縣級(區、縣、縣級市)
ArrayAdapter<String> provinceAdapter = null; //省級介面卡
ArrayAdapter<String> cityAdapter = null; //地級介面卡
ArrayAdapter<String> countyAdapter = null; //縣級介面卡
static int provincePosition = 3;
//省級選項值
private String[] province = new String[] {"北京","上海","天津","廣東"};//,"重慶","黑龍江","江蘇","山東","浙江","香港","澳門"};
//地級選項值
private String[][] city = new String[][]
{
{ "東城區", "西城區", "崇文區", "宣武區", "朝陽區", "海淀區", "豐臺區", "石景山區", "門頭溝區",
"房山區", "通州區", "順義區", "大興區", "昌平區", "平谷區", "懷柔區", "密雲縣",
"延慶縣" },
{ "長寧區", "靜安區", "普陀區", "閘北區", "虹口區" },
{ "和平區", "河東區", "河西區", "南開區", "河北區", "紅橋區", "塘沽區", "漢沽區", "大港區",
"東麗區" },
{ "廣州", "深圳", "韶關" // ,"珠海","汕頭","佛山","湛江","肇慶","江門","茂名","惠州","梅州",
// "汕尾","河源","陽江","清遠","東莞","中山","潮州","揭陽","雲浮"
}
};
//縣級選項值
private String[][][] county = new String[][][]
{
{ //北京
{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},
{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"}
},
{ //上海
{"無"},{"無"},{"無"},{"無"},{"無"}
},
{ //天津
{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"}
},
{ //廣東
{"海珠區","荔灣區","越秀區","白雲區","蘿崗區","天河區","黃埔區","花都區","從化市","增城市","番禺區","南沙區"}, //廣州
{"寶安區","福田區","龍崗區","羅湖區","南山區","鹽田區"}, //深圳
{"武江區","湞江區","曲江區","樂昌市","南雄市","始興縣","仁化縣","翁源縣","新豐縣","乳源縣"} //韶關
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSpinner();
}
/*
* 設定下拉框
*/
private void setSpinner()
{
provinceSpinner = (Spinner)findViewById(R.id.spin_province);
citySpinner = (Spinner)findViewById(R.id.spin_city);
countySpinner = (Spinner)findViewById(R.id.spin_county);
//繫結介面卡和值
provinceAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, province);
provinceSpinner.setAdapter(provinceAdapter);
provinceSpinner.setSelection(3,true); //設定預設選中項,此處為預設選中第4個值
cityAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, city[3]);
citySpinner.setAdapter(cityAdapter);
citySpinner.setSelection(0,true); //預設選中第0個
countyAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, county[3][0]);
countySpinner.setAdapter(countyAdapter);
countySpinner.setSelection(0, true);
//省級下拉框監聽
provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
// 表示選項被改變的時候觸發此方法,主要實現辦法:動態改變地級介面卡的繫結值
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
{
//position為當前省級選中的值的序號
//將地級介面卡的值改變為city[position]中的值
cityAdapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_spinner_item, city[position]);
// 設定二級下拉選單的選項內容介面卡
citySpinner.setAdapter(cityAdapter);
provincePosition = position; //記錄當前省級序號,留給下面修改縣級介面卡時用
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
//地級下拉監聽
citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3)
{
countyAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, county[provincePosition][position]);
countySpinner.setAdapter(countyAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
}
}
要獲取下拉框spinner中選中的值,用下面這方法就OK了
provinceSpinner.getSelectedItem().toString();
citySpinner.getSelectedItem().toString();
countySpinner.getSelectedItem().toString();
主要實現辦法:動態載入各級下拉值的介面卡
在監聽本級下拉框,當本級下拉框的選中值改變時,隨之修改下級的介面卡的繫結值
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="wrap_content"
android:orientation="horizontal"
android:padding="8dp" >
<Spinner
android:id="@+id/spin_province"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spin_city"
android:layout_width="112dp"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spin_county"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
實現程式碼:
複製程式碼 程式碼如下:
package com.example.android.demo.spinner;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity
{
private Spinner provinceSpinner = null; //省級(省、直轄市)
private Spinner citySpinner = null; //地級市
private Spinner countySpinner = null; //縣級(區、縣、縣級市)
ArrayAdapter<String> provinceAdapter = null; //省級介面卡
ArrayAdapter<String> cityAdapter = null; //地級介面卡
ArrayAdapter<String> countyAdapter = null; //縣級介面卡
static int provincePosition = 3;
//省級選項值
private String[] province = new String[] {"北京","上海","天津","廣東"};//,"重慶","黑龍江","江蘇","山東","浙江","香港","澳門"};
//地級選項值
private String[][] city = new String[][]
{
{ "東城區", "西城區", "崇文區", "宣武區", "朝陽區", "海淀區", "豐臺區", "石景山區", "門頭溝區",
"房山區", "通州區", "順義區", "大興區", "昌平區", "平谷區", "懷柔區", "密雲縣",
"延慶縣" },
{ "長寧區", "靜安區", "普陀區", "閘北區", "虹口區" },
{ "和平區", "河東區", "河西區", "南開區", "河北區", "紅橋區", "塘沽區", "漢沽區", "大港區",
"東麗區" },
{ "廣州", "深圳", "韶關" // ,"珠海","汕頭","佛山","湛江","肇慶","江門","茂名","惠州","梅州",
// "汕尾","河源","陽江","清遠","東莞","中山","潮州","揭陽","雲浮"
}
};
//縣級選項值
private String[][][] county = new String[][][]
{
{ //北京
{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},
{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"}
},
{ //上海
{"無"},{"無"},{"無"},{"無"},{"無"}
},
{ //天津
{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"},{"無"}
},
{ //廣東
{"海珠區","荔灣區","越秀區","白雲區","蘿崗區","天河區","黃埔區","花都區","從化市","增城市","番禺區","南沙區"}, //廣州
{"寶安區","福田區","龍崗區","羅湖區","南山區","鹽田區"}, //深圳
{"武江區","湞江區","曲江區","樂昌市","南雄市","始興縣","仁化縣","翁源縣","新豐縣","乳源縣"} //韶關
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSpinner();
}
/*
* 設定下拉框
*/
private void setSpinner()
{
provinceSpinner = (Spinner)findViewById(R.id.spin_province);
citySpinner = (Spinner)findViewById(R.id.spin_city);
countySpinner = (Spinner)findViewById(R.id.spin_county);
//繫結介面卡和值
provinceAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, province);
provinceSpinner.setAdapter(provinceAdapter);
provinceSpinner.setSelection(3,true); //設定預設選中項,此處為預設選中第4個值
cityAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, city[3]);
citySpinner.setAdapter(cityAdapter);
citySpinner.setSelection(0,true); //預設選中第0個
countyAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, county[3][0]);
countySpinner.setAdapter(countyAdapter);
countySpinner.setSelection(0, true);
//省級下拉框監聽
provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
// 表示選項被改變的時候觸發此方法,主要實現辦法:動態改變地級介面卡的繫結值
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
{
//position為當前省級選中的值的序號
//將地級介面卡的值改變為city[position]中的值
cityAdapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_spinner_item, city[position]);
// 設定二級下拉選單的選項內容介面卡
citySpinner.setAdapter(cityAdapter);
provincePosition = position; //記錄當前省級序號,留給下面修改縣級介面卡時用
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
//地級下拉監聽
citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3)
{
countyAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, county[provincePosition][position]);
countySpinner.setAdapter(countyAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
}
}
要獲取下拉框spinner中選中的值,用下面這方法就OK了
provinceSpinner.getSelectedItem().toString();
citySpinner.getSelectedItem().toString();
countySpinner.getSelectedItem().toString();
相關文章
- android 之 Spinner 下拉選單實現級聯Android
- select下拉選單級聯效果例項程式碼
- select級聯下拉選單程式碼例項分析
- jquery 實現層級下拉框聯動效果 程式碼jQuery
- select下拉選單多級級聯效果程式碼例項
- css三級下拉導航選單程式碼例項CSS
- 純CSS實現的二級下拉選單效果程式碼例項CSS
- 動態生成select三級聯動選單程式碼例項
- css二級下拉選單程式碼例項CSS
- jQuery結合PHP+MySQL實現二級聯動下拉選單[例項]jQueryPHPMySql
- Spinner下拉選單
- 模擬實現select下拉選單例項程式碼單例
- CSS3實現的美觀多級下拉選單效果程式碼例項CSSS3
- JS實現級聯下拉選單JS
- 滑鼠懸浮出現下拉選單程式碼例項
- jQuery 省市級聯選單程式碼例項jQuery
- js省市級聯選單程式碼例項JS
- js模擬實現select下拉選單程式碼例項JS
- jquery實現四級級聯下拉選單jQuery
- C# MVC LayUI實現下拉框二級聯動C#MVCUI
- jQuery結合ul和li實現的下拉選單例項程式碼jQuery單例
- 出生日期三級聯動下拉選單
- select下拉選單美化程式碼例項
- javascript實現的三級下拉導航選單JavaScript
- jQuery美化select下拉選單程式碼例項jQuery
- css水平下拉導航選單程式碼例項CSS
- jQuery操作select下拉選單程式碼例項jQuery
- 模擬select下拉選單程式碼例項
- Android UI控制元件系列:Spinner(下拉選單)AndroidUI控制元件
- 淘寶頁面實現(包括級聯選單,下拉選單,框架)框架
- select下拉選單項互換效果程式碼例項
- javascript刪除select下拉選單項程式碼例項JavaScript
- vue使用iview實現單選,禁選,下拉框的效果VueView
- 皕傑報表實現下拉框聯動
- 純CSS實現的二級導航選單效果程式碼例項CSS
- select下拉選單二級聯動
- 原生javascript操作select下拉選單程式碼例項JavaScript
- 模擬美化select下拉選單程式碼例項