編輯:關於Android編程
主要實現辦法:動態加載各級下拉值的適配器
在監聽本級下拉框,當本級下拉框的選中值改變時,隨之修改下級的適配器的綁定值
XML布局:
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; //縣級(區、縣、縣級市) ArrayAdapterprovinceAdapter = null; //省級適配器 ArrayAdapter cityAdapter = null; //地級適配器 ArrayAdapter 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 (MainActivity.this, android.R.layout.simple_spinner_item, province); provinceSpinner.setAdapter(provinceAdapter); provinceSpinner.setSelection(3,true); //設置默認選中項,此處為默認選中第4個值 cityAdapter = new ArrayAdapter (MainActivity.this, android.R.layout.simple_spinner_item, city[3]); citySpinner.setAdapter(cityAdapter); citySpinner.setSelection(0,true); //默認選中第0個 countyAdapter = new ArrayAdapter (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 ( 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 (MainActivity.this, android.R.layout.simple_spinner_item, county[provincePosition][position]); countySpinner.setAdapter(countyAdapter); } @Override public void onNothingSelected(AdapterView> arg0) { } }); } }
provinceSpinner.getSelectedItem().toString();
citySpinner.getSelectedItem().toString();
countySpinner.getSelectedItem().toString();
THE END
debuggerd 簡介 Android系統自帶一個實用的程序異常退出的診斷daemon debuggerd。此進程可以偵測到程序崩潰,並將崩潰時的進程狀態信息輸出到文件
一、概述 這一章先來點有意思的百度地圖應用示例,然後再分章詳細介紹用C#開發Android App的各種基本技術。 本章以百度官網2016年1月發布的
如題,經過三四天的開發嘗試已經初步成型,下面是簡陋的界面圖: 上圖是做的藍牙串口服務的收發界面,主要用於平時的調試之用,由於開發的初衷是為了實現藍牙對單片機的控制,因
本菜開源的一個自己寫的Demo,希望能給Androider們有所幫助,水平有限,見諒見諒… https://github.com/zhiaixinyang