Android——列表选择框(Spinner)

一般状况下,若是列表选择框中要显示的列表项是可知的,那么能够将其保存在数组资源文件中,而后经过数组资源来为列表选择框指定列表项。这样就能够在不编写Java代码的状况下实现一个下拉选择框。java

1.在布局文件中添加一个<spinner>标记,并为其指定android:entries属性,具体代码以下:android

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
	<Spinner android:entries="@array/ctype"
	    	 android:layout_width="wrap_content"
	    	 android:layout_height="wrap_content"
	    	 android:id="@+id/spinner"/>
</LinearLayout>

其中android:entries属性是用来指定列表项的,若是在布局文件中不指定该属性,能够在Java代码中经过为其指定适配器的方式指定;数组

2.编写用于指定列表项的数组资源文件,并将其保存在res\values目录中,这里将其命名为arrays.xml,在该文件中添加一个字符串数组,名称为ctype,具体代码以下app

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <string-array name="ctype">
        <item>ID</item>
        <item>Student Card</item>
        <item>Army Card</item>
        <item>Work Card</item>
        <item>Other</item>
    </string-array>
</resources>

在屏幕上添加列表选择框后,能够使用列表选择框的getSelectedItem()方法获取列表选择框的选中值,能够使用下面的代码:ide

Spinner spinner=(Spinner)findViewById(R.id.spinner);
spinner.getSelectedItem();

若是想要在用户选择不一样的列表项后,执行相应的处理,则能够为该列表选择框添加OnItemSelectedListener事件监听器。例如,为spinner添加选择列表事件监听器,并在onItemSelected()方法中获取选择项的值输出到日志中,能够使用以下代码:布局

package com.basillee.blogdemo;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Spinner spinner=(Spinner)findViewById(R.id.spinner);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View arg1,
					int pos, long id) {
				// TODO Auto-generated method stub
				String result=parent.getItemAtPosition(pos).toString();//获取选择项的值
				Log.i("spinner", result);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

}

下面介绍经过指定适配器的方式指定列表的方式指定列表项的方法。this

(1)建立一个适配器对象,一般使用ArrayAdapter类。在Android中,建立适配器一般能够使用如下两种方法:一种是经过数组资源文件建立;另外一种是经过java里面的字符串数组建立。spa

  • 经过数组资源文件建立适配器,须要使用ArrayAdapter类的createFromResource()方法,具体代码以下:
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.ctype,android.R.layout.simple_dropdown_item_1line);
  • 经过Java代码建立以下
	String[]ctype=new String[]{"ID","Student Card","Army Card"};
     ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,ctype);

(2)为适配器设置下拉列表的选项样式:日志

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

(3)将适配器与选择列表框关联:code

spinner.setAdapter(adapter);
相关文章
相关标签/搜索