一、概述
Spinner是一个每次只能选择所有项的一个项的控件。它的项来自于与之相关联的适配器中。
二、重要属性
android:prompt当Spinner对话框关闭时显示该提示
三、重要方法
setPrompt(CharSequence prompt)设置当Spinner对话框关闭时显示的提示
performClick():如果它被定义就调用此视图的OnClickListener
setOnItemClickListener(AdapterView.OnItemClickListener l):当项被点击时调用
onDetachedFromWindow():当Spinner脱离窗口时被调用。
Android 中文API (38) —— Spinner
http://www.cnblogs.com/over140/archive/2010/11/17/1879794.html
一篇用法讲述的很好的博客:http://blog.csdn.net/applezhengxd/article/details/5573693
另一篇:http://www.cnblogs.com/zziss/archive/2011/12/16/2290367.html
下拉列表 spinner(android)
下拉列表 spinner,这个使用很简单,在 xml 中定义一个标签,然后指定 ArrayAdapter<String> 就可以了,
Adapter 需要指定下拉的布局。关于下拉的数据,书中是写用静态数组,我这里改成了从资源文件中读取
java 代码
import java.util.Map;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class SpinnerTestActivity extends Activity {
/** Called when the activity is first created. */
private TextView iTv;
private Spinner iSpinner;
private String[] items;
private ArrayAdapter<Map<String,Integer>> iAda;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iTv = (TextView) this.findViewById(R.id.tv);
iSpinner = (Spinner) this.findViewById(R.id.spinner);
Resources res = getResources();
items = res.getStringArray(R.array.listItems);
ArrayAdapter<String> ada = new ArrayAdapter<String>( this,
android.R.layout.simple_spinner_item,items);
ada.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
iSpinner.setAdapter(ada);
iSpinner.setOnItemSelectedListener( new Spinner.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
iTv.setText(items[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
main.xml
< 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/tv"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="" />
< Spinner
android:id ="@+id/spinner"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_centerHorizontal ="true"
/>
</ LinearLayout >
string.xml
< resources >
< string name ="hello" >Hello World, SpinnerTestActivity! </ string >
< string name ="app_name" >SpinnerTest </ string >
< string-array name ="listItems" >
< item >A型 </ item >
< item >B型 </ item >
< item >AB型 </ item >
< item >O型 </ item >
</ string-array >
</ resources >