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
				
			}
        	
		});
    }

结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值