<span style="font-family: 'Microsoft YaHei'; background-color: rgb(255, 255, 255);"> 使用Spinner实现下拉列表</span>
<span style="font-family: 'Microsoft YaHei'; background-color: rgb(255, 255, 255);"> 使用Spinner实现下拉列表</span>
目标:1.了解Spinner下拉列表的使用和功能
2.学会使用系统默认的Spinner
3.学会使用自定义样式的Spinner
先来看Spinner的效果图:
执行步骤:
代码实现:
package com.mengmeng.android_spinner;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
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 MainActivity extends Activity implements OnItemSelectedListener {
private TextView textView;
private Spinner spinner;
private List<String> list;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
spinner = (Spinner) findViewById(R.id.spinner);
textView.setText("您当前选择的是北京");
//1.设置数据源
list = new ArrayList<String>();
list.add("北京");
list.add("上海");
list.add("南京");
list.add("广州");
list.add("深圳");
//2.新建ArrayAdapter(数组适配器)
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
//3.为下拉列表设置一个下拉菜单的样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//4.将适配器加载到下拉菜单
spinner.setAdapter(adapter);
//5.给spinner设置监听器
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
//通过Adapter获取当前的信息
String cityName = adapter.getItem(position);
textView.setText("您当前选择的是:"+cityName);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
利用SimpleAdapter实现自定义样式Spinner
效果图如下:
代码实现如下:
package com.mengmeng.android_spinner2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView myTextView;
private Spinner mySpinner;
private List<Map<String, Object>> dataList;
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.textView);
mySpinner = (Spinner) findViewById(R.id.spinner);
myTextView.setText("您选择的是北京");
//1.设置数据源,添加一个下拉列表list,这里添加的项就是下拉列表的菜单项
dataList = new ArrayList<Map<String,Object>>();
getDate();
//2.添加适配器
adapter = new SimpleAdapter(this, dataList, R.layout.item,
new String[]{"image","text"}, new int[]{R.id.item_image,R.id.item_text});
//3.为适配器设置下拉样式
adapter.setDropDownViewResource(R.layout.item);
//4.添加适配器
mySpinner.setAdapter(adapter);
//5.添加下拉菜单的响应事件
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position ,long id) {
Object o = adapter.getItem(position);
myTextView.setText("您选择的是"+o);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
myTextView.setText("None");
}
});
}
private void getDate(){
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("image", R.drawable.ic_launcher);
map1.put("text", "北京");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("image", R.drawable.ic_launcher);
map2.put("text", "上海");
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("image", R.drawable.ic_launcher);
map3.put("text", "南京");
Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("image", R.drawable.ic_launcher);
map4.put("text", "广州");
Map<String, Object> map5 = new HashMap<String, Object>();
map5.put("image", R.drawable.ic_launcher);
map5.put("text", "深圳");
dataList.add(map1);
dataList.add(map2);
dataList.add(map3);
dataList.add(map4);
dataList.add(map5);
}
}<strong>
</strong>
自定义的样式:item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@drawable/ic_launcher"/>
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/item_image"
android:text="None"/>
</RelativeLayout><strong>
</strong>