android组建学习之Spinner(列表选择框控件)

 

【实验目的】
    掌握Spinner的功能及其使用方法;
    掌握Spinner选择事件监听的方法。
【实验原理】
1.Spinner(列表选择框)
    Android中的Spinner列表选择框,就是平时常见的下拉列表框,通常用于提供一系列的可选择的列表项,供用户进行选择,方便用户输入。具体效果如图1所示。

图1
Spinner下拉列表中的列表项数据,可以来自于资源数组,也可以通过Adapter获取数据。

2.Spinner(列表选择框)常用XML属性

XML属性    描述
android:dropDownWidth    wrap_content 布满整个anctor
match_content 布满整个屏幕
android:dropDownHorizontalOffset     下拉模式的水平间距?
android:dropDownSelector    下拉模式时候的选择器
android:gravity    当前选中项的
android:popupBackground    下拉模式的弹出背景
android:prompt    提示:类似于title
android:spinnerMode    模式:是下拉还是弹出一个dialog
android:entries    通过资源数组为其指定列表项

【实验内容】    
目标1:通过资源数组,实现Spinner;
目标2:通过Adapter,实现Spinner

练习一:通过资源数组,实现Spinner
    这种实现方式,适用于下拉列表的内容是事先知道,且固定的情况。
【实验步骤】
1.创建新项目
先建立一个空项目,如HelloWorld项目,然后进行以下修改。

2.UI设计
    修改主布局文件activity_main.xml,在其中添加Spinner控件、TextView控件、Button控件等,修改后的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="证件类型 "
        android:textSize="20sp"
        android:textColor="#0000ff"/>
    <Spinner android:id="@+id/autoText"
        android:entries="@array/auto"
        android:layout_alignBottom="@id/tv"
        android:layout_toRightOf="@id/tv"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>
    <Button android:id="@+id/bt"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@id/autoText"
        android:layout_alignParentRight="true"
        android:text="确定" />
</RelativeLayout>
    在Spinner控件中,通过android:entries设定资源数组。 

3.资源建设
修改res/value目录下的strings.xml文件,建立资源数组,使其内容如下:
<resources>
    <string name="app_name">MyAutoComplete</string>
    <string-array name="auto" >
        <item>身份证</item>
        <item>学生证</item>
        <item>军人证</item>
        <item>工作证</item>
        <item>其他</item>
    </string-array>
</resources>
    其中,添加了数组auto,其内容即为下拉列表的表项。

4.编写代码
    修改MainActivity.java,修改后的内容如下:
package com.example.administrator.myautocomplete;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spinner=(Spinner)findViewById(R.id.autoText);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                String[] autoInfo = getResources().getStringArray(R.array.auto);
                Toast.makeText(MainActivity.this, "你点击的是:"+autoInfo[pos], 2000).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }
}
程序说明:
(1)获取Spinner对象,并对点击事件进行监听处理。
        Spinner spinner=(Spinner)findViewById(R.id.autoText);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                String[] autoInfo = getResources().getStringArray(R.array.auto);
                Toast.makeText(MainActivity.this, "你点击的是:"+autoInfo[pos], 2000).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
如果不对选择事件进行监听的话,可以不用对代码进行修改。

5.效果验证
    编译运行,点击下拉按钮,效果如图1所示。

练习二:通过Adapter,实现Spinner
    这种实现方式在运行时动态地确定下拉列表的内容,程序可以对下拉列表的下拉项进行定制。
【实验步骤】
1.创建新项目
先建立一个空项目,如HelloWorld项目,然后进行以下修改。

2.UI设计
    修改主布局文件activity_main.xml,在其中添加Spinner控件、TextView控件、Button控件等,修改后的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="证件类型 "
        android:textSize="20sp"
        android:textColor="#0000ff"/>
    <Spinner android:id="@+id/autoText"
        android:layout_alignBottom="@id/tv"
        android:layout_toRightOf="@id/tv"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>
    <Button android:id="@+id/bt"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@id/autoText"
        android:layout_alignParentRight="true"
        android:text="确定" />
</RelativeLayout>
    下拉列表中的内容,由代码生成。所以不需要在Spinner控件中设置android:entries属性。

3.编写代码
    修改MainActivity.java,修改后的内容如下:
package com.example.administrator.myautocomplete;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    String[] autoInfo=new String[]{"身份证","学生证","军人证","工作证","其他"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayAdapter<String> myAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,autoInfo);

        Spinner spinner=(Spinner)findViewById(R.id.autoText);
        spinner.setAdapter(myAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                Toast.makeText(MainActivity.this, "你点击的是:"+autoInfo[pos], 2000).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }
}
程序说明:
(1)准备下拉列表中的数据    
    将列表项数据放在一个数组中。
String[] autoInfo=new String[]{"身份证","学生证","军人证","工作证","其他"};
(2)建立Adapter
建立一个ArrayAdapter,并将其与数据关联。
ArrayAdapter<String> myAdapter=new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,
autoInfo);
此处,android.R.layout.simple_spinner_dropdown_item是系统自带的布局。
(3)获取Spinner控件对象,并将之与Adapter关联
Spinner spinner=(Spinner)findViewById(R.id.autoText);
        spinner.setAdapter(myAdapter);
(4)点出事件监听处理
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
     @Override
     public void onItemSelected(AdapterView<?> parent, View view,int pos, long id) {
               Toast.makeText(MainActivity.this, "你点击的是:"+autoInfo[pos], 2000).show();
     }
     @Override
     public void onNothingSelected(AdapterView<?> parent) {
         // Another interface callback
     }
 });

4.效果验证
    编译运行,点击图片列表的某一项,可得到效果如图2所示。

图2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值