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
}
});
}
结果: