09 - android Spinner 下拉列表

目标:

      掌握下拉列表Spinner的使用;

      可以通过程序配置Spinner显示内容;

      可以通过配置文件配置Spinner显示内容

通过本程序就应该发现,Spinner的核心问题就在于下啦数据内容的显示上。

下拉列表框也是一种常见的图形组件,它可以为用户提供列表的选择方式,与复选框或单选按钮相比还可以节省手机屏幕空间,在android中可以使用android.widget.Spinner类实现:

java.lang.Object
   ↳android.view.View
   ↳android.view.ViewGroup
   ↳android.widget.AdapterView<T extends android.widget.Adapter>
   ↳android.widget.AbsSpinner
   ↳android.widget.Spinner

在Spinner类中的setAdapter()方法,表示的是设置下拉列表框的显示内容

SIpnnerAdapter的功能是一个适配器,会将锁需要的数据进行包装并放在Spinner之中。

既然现在对Spinner的核心问题在于内容的配置上,所以这里面就需要通过一下方式设置内容

配置列表项:

  在android中,可以直接在main.xml文件中定义”<Spinner>“节点,但是在定义此原生的时候却不能直接设置其显示的列表项,关于下拉列表框中的列表项有一下两种方式进行配置:

   方式一。直接通过资源文件配置:

  方式二:通过android.widget.ArrayAdapter类读取资源文件或是指定具体设置的数据;

   对于Spinner的内容可以直接在values文件夹之中定义若干个资源文件,例如,例如定义城市信息的下拉列表框,就可以定义一个city_data.xml

例如:方法一。

main.xml

 

<?xml version="1.0" encoding="utf-8"?>

<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/info_city"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="请选择你喜欢的城市:" />


    <Spinner

        android:id="@+id/mycity"

        android:prompt="@string/city_name"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:entries="@array/city_labels"

        />

</LinearLayout>

values包下面增加city_data.xml

 

<?xml version="1.0" encoding="utf-8"?>

<resources>


    <string-array name="city_labels">

        <item>北京</item>

        <item>南京</item>

        <item>上海</item>

    </string-array>


</resources>

 

方法二:通过android.widget.ArrayAdapter类

ArrayAdapter类的功能:有两个主要的功能:读取资源文件中定义的列表项,或者通过List集合设置列表项,此类中定义了如下几个常用的方法:

即:如果要使用ArrayAdapter配置下拉列表的内容,可以采用配置文件完成,为了演示以上两种实现形式,下面先采用配置文件读取方式完成。

例如:定义表示颜色下拉框:

       新建color_data.xml

 

<?xml version="1.0" encoding="utf-8"?>

<resources>


    <string-array name="color_labels">

        <item>红色</item>

        <item>绿色</item>

        <item>黄色</item>

    </string-array>


</resources>

这个配置文件将在ArrayAdapter类中进行读取。为了能够使用ArrayAdapter还需要定义spinner;

main.xml

 

 

    <TextView

        android:id="@+id/info_color"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="请选择你喜欢的颜色:" />


    <Spinner

        android:id="@+id/mycolor"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"/>

现在文件中不再添加任何信息,只是定义空的下拉列表框,而后在Activity程序中,要动态的配置

 

ArrayAdapter

extends BaseAdapter
implements Filterable

<!-- end header -->

java.lang.Object
   ↳android.widget.BaseAdapter
   ↳

android.widget.ArrayAdapter<T>

 

 

 

 

public static ArrayAdapter<CharSequence> createFromResource (Context context, int textArrayResId, int textViewResId)
Since: API Level 1

Creates a new ArrayAdapter from external resources. The content of the array is obtained through getTextArray(int).

 

用该方法读取所需要的信息

Activity类操作:

 

package com.sun.android;


import android.app.Activity;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.Spinner;


public class HelloWorld extends Activity {

private Spinner spiColor = null;// 表示要读取的颜色列表框

private ArrayAdapter<CharSequence> adapterColor = null;// 所有的数据都是String


/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);// 声明周期方法

super.setContentView(R.layout.main);// 设置要使用的布局管理器

this.spiColor = (Spinner) super.findViewById(R.id.mycolor);

this.spiColor.setPrompt("请选择你喜欢的颜色:");

this.adapterColor = ArrayAdapter.createFromResource(this,

R.array.city_labels, android.R.layout.simple_spinner_item);// 实例化ArrayAdapter

this.adapterColor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//换个风格

this.spiColor.setAdapter(this.adapterColor);//设置显示信息

}

}

 

    可以对于ArrayAdapter而言,除了读取资源文件之外,还可能需要通过程序动态生成,所以现在可以使用ArrayAdapter的另一种形式

   部分信息之后再慢慢写

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值