Spinner是一个下拉列表,通常用于选择一系列可选择的列表项,它可以使用适配器,也可以直接设置数组源。
"@+id/ctype"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/ctype"/>//设置数组源
spinner.setSelection(2,true);选中默认值
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view , int position, long id) {
//当用户选择其中一项时
}
@Override
public void onNothingSelected(AdapterView> parent) {
//当没有任何选择时
}
});
它也可以设置适配器作为数据源,如果下拉列表中并不止一个TextView显示文本,那么就需要设置适配器,但是一般下拉列表都只需要一个文本。
spinner.setAdapter(adapter); 如果需要多个组件值都需要改变,那么一般使用SimpleAdapter类。
都是有点类似下拉效果而已,但是实际用途是非常不一样的;
1.自动完成文本框继承于EditText,是输入内容时的提示作用。
2.列表选择框一般用于点击一个文本框,然后显示可以选择的列表内容,点击列表某个内容后把数据显示在文本框内。
<resources>
<string-array name="myarray">
<item>李嘉诚item>
<item>李文志item>
<item>李世民item>
<item>李鸿章item>
<item>李丹丹item>
<item>李泉item>
string-array>
resources>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/myarray" />
RelativeLayout>
public class MainActivity extends Activity {
// Spinner对象
Spinner spinner;
// 数据源
String arr[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 实例化对象
spinner = (Spinner) findViewById(R.id.spinner);
//获取资源文件里面的数组数据
arr=getResources().getStringArray(R.array.myarray);
// 添加选中条目的点击事件
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
// 选中任意的条目后会触发
@Override
public void onItemSelected(AdapterView> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "你选中了" + arr[position], 0)
.show();
}
// 这个方法基本没有用
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
RelativeLayout>
package com.example.lesson7_spinner;
import android.R.integer;
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.SpinnerAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
// Spinner对象
Spinner spinner;
// 数据源
String[] array = { "张三", "张4", "张5", "张6", "张7", "张8", "张9", "张10", "张11" };
String arr[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 实例化对象
spinner = (Spinner) findViewById(R.id.spinner);
// 添加选中条目的点击事件
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
// 选中任意的条目后会触发
@Override
public void onItemSelected(AdapterView> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "你选中了" + arr[position], 0)
.show();
}
// 这个方法基本没有用
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
//创建适配器对象
SpinnerAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_dropdown_item_1line, array);
//给Spinner设置适配器
spinner.setAdapter(adapter);
}
}
程序运行后点击下拉后显示的界面:
在一般的程序中第二种方法的使用是常见的,因为页面数据的传递一般都是集合或数组。