Android 的下拉列表框使用
下拉列表框也是一種常用的組件,形狀類似于html的select。功能的話和那個單選按鈕有些類似。這次的話就使用這個下拉列表框做一個城市和區域聯動的小例子,數據的話簡單起見就弄成靜態的。就不先扯淡了。
1.先來看下下拉列表框的標簽
<Spinner android:id="@+id/city" android:prompt="@string/city_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/city_name" />
這里要注意的就是2處,prompt是列表框的提示如圖所示,entries當然就是下拉的內容了,也就是下面顯示的上海、南京、昆明
2.下面就開始一步一步的寫這個小例子了,首先寫一下靜態的數據。我打算把城市寫在一個xml文件里。下面是我的xml文件這個文件就放在values文件夾下
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="city_name"> <item>上海</item> <item>南京</item> <item>昆明</item> </string-array> </resources>
為了讓大家看的更清楚,我把strings.xml也貼出來了
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, SpinnerActivity!</string> <string name="app_name">spinner</string> <string name="city_label">選擇城市</string> <string name="area_label">選擇城市</string> </resources>
城市的下拉沒問題了剩下的就是,區域的級聯了,在這里區域就把他放在一個數組里。這里就要用到ArrayAdapter了,對應的事件就是下拉改變事件了這個事件也有一個監聽器叫做setOnItemSelectedListener。聯動效果是怎么來的呢,其實很簡單只需要城市下拉改變我們就把對應的區域動態的添加到二級下拉就OK了。這個操作就需要ArrayAdapterd的支持了。下面先把ArrayAdapter和區域定義一下
//定義一個字符數組,用來填充區域 private String[][] areaData=new String[][]{{"浦東新區","徐匯區","普陀區","楊浦區"},{"雨花臺區","白下區"},{"西山區","北市區"}}; //定義下拉列表適配器,用于填充內容 private ArrayAdapter<CharSequence> adapterArea=null;
下面就是這個Adapter的用法了
//實例化列表項 adapterArea=new ArrayAdapter<CharSequence>(SpinnerActivity.this,android.R.layout.simple_spinner_item,SpinnerActivity.this.areaData[position]); //設置列表顯示風格 adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); area.setAdapter(adapterArea);
對于第一句,第一個參數就是上下文,第二個參數就是下拉框的顯示風格,第三個就是我們要填到下拉的數據了
第二句就是設置下拉框內容的風格了,不寫的話顯示的是這個樣子
寫了的話是這個樣子
說了這么多我就把全部的代碼貼出來了,里面也夾雜著一個圖片的長按事件這個操作可以切換手機桌面,換桌面的時候不要忘了授權
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
下面是java代碼
package org.lxh.spinner; import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.Spinner; import android.widget.Toast; public class SpinnerActivity extends Activity { private ImageView image=null; private Spinner city=null; private Spinner area=null; //定義一個字符數組,用來填充區域 private String[][] areaData=new String[][]{{"浦東新區","徐匯區","普陀區","楊浦區"},{"雨花臺區","白下區"},{"西山區","北市區"}}; //定義下拉列表適配器,用于填充內容 private ArrayAdapter<CharSequence> adapterArea=null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); city=(Spinner)this.findViewById(R.id.city); area=(Spinner)this.findViewById(R.id.area); image=(ImageView)this.findViewById(R.id.wall); image.setOnLongClickListener(new View.OnLongClickListener() { public boolean onLongClick(View v) { //設置桌面之前要先清除單前的桌面 try { SpinnerActivity.this.clearWallpaper(); SpinnerActivity.this.setWallpaper(image.getResources().openRawResource(R.drawable.fengj)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } }); area.setOnItemSelectedListener(new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(SpinnerActivity.this, parent.getItemAtPosition(position).toString(), 1).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); city.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //實例化列表項 adapterArea=new ArrayAdapter<CharSequence>(SpinnerActivity.this,android.R.layout.simple_spinner_item,SpinnerActivity.this.areaData[position]); //設置列表顯示風格 adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); area.setAdapter(adapterArea); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } }
下面補上main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Spinner android:id="@+id/city" android:prompt="@string/city_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/city_name" /> <Spinner android:id="@+id/area" android:prompt="@string/area_label" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/wall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/fengj" /> </LinearLayout>
來自:http://blog.csdn.net/chenwill3/article/details/8249104
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!