【Android开发】基本组件-列表选择框

来源:互联网 发布:suse linux snmp 安装 编辑:程序博客网 时间:2024/04/29 14:20
Android中提供的列表选择框(Spinner)相当于在网页中常见的下拉列表框,通常用于提供一系列可选择的列表项共用户选择。

Android中,可以使用两种方法向屏幕中添加列表选择框,一种是通过在XML布局文件中使用<Spinner>标记添加;另一种是在Java文件中,通过new关键字创建。

推荐使用XML配置,基本语法如下:
<Spinner    android:prompt="@string/info"    android:entries="@array/数组名称"    android:layout_width="warp_content"    android:layout_height="warp_content"    android:id="@+id/ID号"></Spinner>

其中,android:entries为可选属性,用于指定列表项,如果在布局文件中不指定该属性,可以在JAVA代码中通过为其指定适配器的方式指定;android:prompt属性也是可选属性,用于指定列表选择框的标题。

实例:
res/layout/main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:screenOrientation="landscape"android:background="#FFFFFF"><Spinner android:entries="@array/ctype"    android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:id="@+id/spinner1"/></LinearLayout>

编写用于指定列表项的数组资源文件,并将其保存在res/values目录中,这里将其命名为arrays.xml,在该文件中添加一个字符串数组,命名为ctype,具体代码如下:
<?xml version="1.0" encoding="utf-8"?><resources>    <string-array name="ctype">        <item>身份证</item>        <item>学生证</item>        <item>军人证</item>        <item>工作证</item>        <item>其他</item>    </string-array></resources>

MainActivity:
package com.example.test;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.Toast;public class MainActivity extends Activity {private OnCheckedChangeListener checkBox_listener;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);    }}

效果如图:


可以用下列方法获取列表选择框中的选中值:
Spinner spinner=(Spinner)findViewById(R.id.spinner1);spinner.getSelectedItem();

如果需要在用户选择不同的列表项后,执行相应的处理,则可为该列表选项添加OnItemSelecedListener事件监听器。这里我们获取选择项的值输出到日志中。

//为选择列表框添加OnItemSelecedListener事件监听器spinner.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View v, int pos,long id) {String result=parent.getItemAtPosition(pos).toString();Log.i("Spinner实例", result);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});

点击后的结果如图:



在使用列表选择框时,如果不在布局文件中直接为其指定要显示的列表项,也可以通过为其指定适配器的方式指定。下面的例子介绍通过指定适配器的方式指定列表项的方法。


为列表选择框指定适配器,通常分为一下3个步骤实现:
1.创建一个适配器对象,通常使用ArrayAdapter类。在Android中,创建适配器通常可以使用以下两种方法:一种是通过数组资源文件创建;另一种是通过在Java文件中使用字符串数组创建。
a.通过数组资源文件创建适配器,需要使用ArrayAdapter类的实例,具体代码如下:
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.ctype, 
android.R.layout.simple_spinner_item); //创建一个适配器

b.通过在Java文件中使用字符串数组创建
String [] ctype=new String[]{"身份证","学生证","工作证","其他"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ctype);

2.为适配器设置列表框下拉时的选项样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

3.将适配器与选择列表框关联

spinner.setAdapter(adapter);

转载请注明出处:http://blog.csdn.net/acmman/article/details/44805211

0 0
原创粉丝点击