Spinner監聽與級聯

         Spinner學習中一個簡單的例子。

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/city_prompt" />
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <Spinner 
        android:id="@+id/city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:prompt="@string/city_prompt"
        android:entries="@array/city_info"/>
    <Spinner 
        android:id="@+id/area"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:prompt="@string/area_prompt"
        />
	</LinearLayout>
	
</LinearLayout>


string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, SpinnerListenerActivity!</string>
    <string name="app_name">SpinnerListener</string>
	<string name="city_prompt">請選擇您喜歡的城市:</string>
	<string name="area_prompt">請選擇您喜歡的城區:</string>
</resources>

city_data.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string-array name="city_info">
	    <item >中國--武漢</item>
	    <item >中國--四川</item>
	    <item >中國--上海</item>
	</string-array>
</resources>

SpinnerListenerActivity.java:

package com.sample.spinnerlistener;

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.Spinner;
import android.widget.TextView;

public class SpinnerListenerActivity extends Activity {
	private TextView info = null;
	private Spinner city = null;
	private Spinner area = null;
	private ArrayAdapter<CharSequence> areaAdapter = null;//添加到spinner area中
	private String[][] areaData = new String[][]{//保存城區信息,與一級列表中的順序相同
			{"武昌","漢陽","漢口"},
			{"成都","資陽","樂山"},
			{"黃浦","楊浦","閔行"}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.main);
        
        info = (TextView)this.findViewById(R.id.info);//獲取TextView組件
        city = (Spinner)this.findViewById(R.id.city);//獲取spanner組件
        area = (Spinner)this.findViewById(R.id.area);
        
        /**
         * 爲spinner註冊監聽事件,當選項改變時觸發,並將選項內容在TextView上顯示
         */
        city.setOnItemSelectedListener(new OnItemSelectedListener() {
        	/**
        	 * Callback method to be invoked when an item in this view has been selected.
        	 *  This callback is invoked only when the newly selected position is different from the previously selected position or if there was no selected item.
        	 * 
        	 *  Impelmenters can call getItemAtPosition(position) if they need to access the data associated with the selected item.
			 *	Parameters
				

			 *	parent	The AdapterView where the selection happened
			 *	view	The view within the AdapterView that was clicked
			 *	position	The position of the view in the adapter
			 *	id	The row id of the item that is selected
        	 */
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				/**
				 * 實例化Adapter對象,並將一級列表中當前選中項所對應的子菜單內容areaData[position]封裝到Adapter對象中
				 */
				areaAdapter = new ArrayAdapter<CharSequence>(SpinnerListenerActivity.this, android.R.layout.simple_spinner_item, areaData[position]);
				area.setAdapter(areaAdapter); //設置二級列表的內容
				
				String text = parent.getItemAtPosition(position).toString();//獲取一級列表中選中項的內容
				info.setText("您選擇的城市是:"+text);
				
			}
			//沒有選中項是觸發
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
        	
		});
    }

結果:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章