首先在布局文件中创建Spinner 控件,一个最简单的Spinner 控件如下:
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
然后再在JAVA代码中初始化Spinner,为其设置适配器和监听事件。
1. 首先用findViewById() 的方法初始化Spinner
public class MainActivity extends Activity {
<strong>private Spinner spinner;</strong>
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<strong>spinner = (Spinner)findViewById(R.id.spinner);</strong>
...
}
}
2. 为其设置适配器。这里有两种方法,第一种是使用ArrayAdapter,第二种是使用SimpleAdapter,两种适配器的使用方法与在ListView 中基本相同
1)使用ArrayAdapter
准备数据源,创建适配器,绑定适配器
public class MainActivity extends Activity {
...
private ArrayAdapter<String> adapter1;
private List<String> dataList1;
private String[] cities = {"北京", "上海", "深圳", "广州"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
dataList1 = new ArrayList<String>();
for(int i = 0; i < cities.length; i++){
dataList1.add(cities[i]);
}
adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, dataList1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter2);
...
}
}注意到,这里使用适配器多了一条方法
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)这个方法专门用来给Spinner配置下来的样式,这里选择使用了系统的样式。2)使用SimpleAdapter
准备目标项布局,准备数据源,创建适配器,绑定适配器
spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/spinner_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:text="text" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {SimpleAdapter也可以用setDropDownViewResource方法给适配器设置下拉的样式,不过必须要使用自定义的样式。
...
private SimpleAdapter adapter2;
private List<Map<String, Object>> dataList2;
private String[] cities = {"北京", "上海", "深圳", "广州"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
dataList2 = new ArrayList<Map<String, Object>>();
for(int i = 0; i < cities.length; i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", R.drawable.ic_launcher);
map.put("text", cities[i]);
dataList2.add(map);
}
adapter2 = new SimpleAdapter(this, dataList2, R.layout.spinner_item, new String[]{"image", "text"}, new int[]{R.id.image, R.id.spinner_text});
spinner.setAdapter(adapter2);
...
}
}3. 设置监听器
设置监听器使用SetOnItemSelectedListener 方法和OnItemSelectedListener 接口
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
...
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
...
}
});
一个完整的示例代码如下
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.spinnerdemo.MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/spinner_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:text="text" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private TextView text;
private Spinner spinner;
//private ArrayAdapter<String> adapter1;
private SimpleAdapter adapter2;
//private List<String> dataList1;
private List<Map<String, Object>> dataList2;
private String[] cities = {"北京", "上海", "深圳", "广州"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.text);
spinner = (Spinner)findViewById(R.id.spinner);
text.setText("您选择的是北京");
//dataList1 = new ArrayList<String>();
//for(int i = 0; i < cities.length; i++){
//dataList1.add(cities[i]);
//}
//adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, dataList1);
dataList2 = new ArrayList<Map<String, Object>>();
for(int i = 0; i < cities.length; i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", R.drawable.ic_launcher);
map.put("text", cities[i]);
dataList2.add(map);
}
adapter2 = new SimpleAdapter(this, dataList2, R.layout.spinner_item, new String[]{"image", "text"}, new int[]{R.id.image, R.id.spinner_text});
//adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter2);
//adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
text.setText("您选择的是" + adapter2.getItem(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}
效果如下: