編輯:關於Android編程
主要實現辦法:動態加載各級下拉值的適配器
在監聽本級下拉框,當本級下拉框的選中值改變時,隨之修改下級的適配器的綁定值
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();
THE END
移動端開發時,我們常使用chrome自帶的模擬器,模擬各種手機設備。 但模擬畢竟是模擬,當開發完畢,使用真機訪問頁面出現問題時如何調試呢?下面介紹一種針對android機
Service概念及用途:Android中的服務,它與Activity不同,它是不能與用戶交互的,不能自己啟動的,運行在後台的程序,如果我們退出應用時,Service進程
首先給大家看一下我們今天這個最終實現的效果圖: 這個是ios中的反彈效果。當然我們安卓中如果想要實現這種效果,感覺不會那麼生硬,滾動到底部或者頂部的時候。當然使
Android插件開發初探 對於Android的插件化其實已經討論已久了,但是市面上還沒有非常靠譜成熟的插件框架供我們使用。這裡我們就嘗試性的對比一下Java中,我們使用