Android开发(21)--有关Spinner控件的使用说明
下拉列表 Spinner,Spinner是一个每次只能选择所有项的一个项的控件。它的项来自于与之相关联的适配器中。
Spinner的使用,可以极大提高用户的体验性。当需要用户选择的时候,可以提供一个下拉列表将所有可选的项列出来。供用户选择。
一、使用数组作为数据源
布局文件:
<RelativeLayout 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: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=".MainActivity" > <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> </RelativeLayout>
MainActivity.java
package com.example.lesson16_spinner; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.SimpleAdapter; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends Activity { // 声明spinner对象 private Spinner spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = (Spinner) findViewById(R.id.spinner1); //使用数组作为数据源 final String arr[] = new String[] { "周一", "周二", "周三", "周四", "周五", "周六", "周七" }; // adpater对象 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr); List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("logo", R.drawable.ic_launcher); map.put("text", "xxxx说明操作"); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("logo", R.drawable.ic_launcher); map2.put("text", "xxx说明xxx"); data.add(map); data.add(map2); SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.activity_item, new String[] { "logo", "text" }, new int[] { R.id.imageView1, R.id.textView1 }); // 设置显示的数据 spinner.setAdapter(arrayAdapter); //spinner.setAdapter(simpleAdapter); Toast.makeText(getApplicationContext(),"main Thread"+ spinner.getItemAtPosition(spinner.getSelectedItemPosition()), Toast.LENGTH_LONG).show(); // 注册事件 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // String content = arr[position]; Spinner spinner = (Spinner) parent; Toast.makeText(getApplicationContext(), "xxxx" + spinner.getItemAtPosition(position), Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(getApplicationContext(), "没有改变的处理", Toast.LENGTH_LONG).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
效果如下:
![](//img-blog.csdn.net/20130624234306390?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2d4NDI1MDIxMjM0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
二、使用XML作为数据源(非本人代码)
布局文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="plantes"> <item>NOKIA</item> <item>MOTO</item> <item>HTC</item> <item>LG</item> <item>其他</item> </string-array> </resources>
java代码:
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.TextView; public class SpinnerActivity extends Activity { private TextView view2; private Spinner spinner2; private ArrayAdapter adapter2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.spinner); spinner2 = (Spinner) findViewById(R.id.spinner02); view2 = (TextView) findViewById(R.id.spinnerText02); //将可选内容与ArrayAdapter连接起来 adapter2 = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item); //设置下拉列表的风格 adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //将adapter2 添加到spinner中 spinner2.setAdapter(adapter2); //添加事件Spinner事件监听 spinner2.setOnItemSelectedListener(new SpinnerXMLSelectedListener()); //设置默认值 spinner2.setVisibility(View.VISIBLE); } //使用XML形式操作 class SpinnerXMLSelectedListener implements OnItemSelectedListener{ public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { view2.setText("你使用什么样的手机:"+adapter2.getItem(arg2)); } public void onNothingSelected(AdapterView<?> arg0) { } } }