5使用Spinner实现下拉列表
5-1 步骤
5-2 实现下拉列表
MainActivity.java
package com.example.andriod2_spinner; import java.util.ArrayList; 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.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.text); textView.setText("您选择的是"); spinner=(Spinner)findViewById(R.id.spinner); // 1.设计数据源 list=new ArrayList<String>(); list.add("上海"); list.add("广州"); list.add("深圳"); list.add("北京"); // 2.新建ArrayaAdapter(数组适配器) adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list); // 3.adapter设置一个下拉列表样式 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 4.spinner加载适配器 spinner.setAdapter(adapter); // 5.spinner设置监听器 spinner.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub String cityName=adapter.getItem(position); //String cityName1=list.get(position); textView.setText("您选择的是"+cityName); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }
Activity_mian.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/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#ff0000" android:textSize="25sp" /> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
5-3 利用SimpleAdapter实现自定义样式Spinner
MainActivity.java
package com.example.spinner; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.SimpleAdapter; import android.widget.Spinner; import android.widget.TextView; import android.os.Build; public class MainActivity extends Activity { private TextView myTextView; private Spinner mySpinner; private SimpleAdapter adapter; private List<Map<String, Object>> dataList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 第一步:添加一个下拉列表项的list,这里添加的项就是下拉列表的菜单项 dataList = new ArrayList<Map<String, Object>>(); getData(); myTextView = (TextView) findViewById(R.id.textView); mySpinner = (Spinner) findViewById(R.id.spinner); // 第二步:为下拉列表定义一个适配器,这里就用到里前面定义的list。 adapter=new SimpleAdapter(this, dataList, R.layout.item, new String[]{"image","text"}, new int[]{R.id.image,R.id.text}); // 第三步:为适配器设置下拉列表下拉时的菜单样式。 adapter.setDropDownViewResource(R.layout.item); // 第四步:将适配器添加到下拉列表上 mySpinner.setAdapter(adapter); // 第五步:为下拉列表设置各种事件的响应,这个事响应菜单被选中 mySpinner .setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub /* 将所选mySpinner 的值带入myTextView 中 */ myTextView.setText("您选择的是:" + adapter.getItem(arg2)); } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub myTextView.setText("NONE"); } }); } private void getData() { // TODO Auto-generated method stub Map<String, Object> map = new HashMap<String, Object>(); map.put("image", R.drawable.ic_launcher); map.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", "深圳"); dataList.add(map); dataList.add(map2); dataList.add(map3); dataList.add(map4); } }
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/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/image" android:text="None" /> </RelativeLayout>
Main.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="vertical" > <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="你选择的是" android:textSize="25sp" android:textColor="#ff0000" > </TextView> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" > </Spinner> <!-- 定义一个下拉菜单 --> </LinearLayout>