zoukankan      html  css  js  c++  java
  • Spinnerd的功能和用法

         此处的Spinner其实是一个列表选择框。不过Android的列表选择框并不是需要下拉列表的,而是相当于弹出一个菜单供用户选择。

         Spinner和Gallery都继承了AbsSpinner,AbsSpinner继承了AdapterView,因此它也表现出AdpterView的特征;主要为AdapterView提供Adapter即可。

         实例:让用户选择、

         如果我们使用Spinner时已经可以确定下拉列表里的列表项,则完全不需要编写代码,只需要为Spinner指定android:entries属性即可实现Spinner;如果程序需要在运行时动态的决定Spinner的列表项,或程序需要对Spinner的列表项进行定制,则可使用Adapter为Spinner提供列表项。

         如下布局文件中定义了来两个Spinner组件,其中第一个Spinner组将指定了android:entries属性,第二个Spinner组件没有指定android:entries属性,因此需要在Activity中为它设置Adapter。

         该实例的界面布局文件如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
      >
    <!-- 定义一个Spinner组件,指定显示该Spinner组件的数据 -->
    <Spinner android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:entries="@array/books"
             android:prompt="@string/tip"/>
    
    <Spinner android:id="@+id/spinner"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:prompt="@string/tip"/>"
    </LinearLayout>

    上面的界面布局文件中第一个Spinner使用@array/books指定数组资源。因此需要在res/value目录下使用XML文件来定义一份数组资源,该数组资源文件的内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="books">
            <item >疯狂Java讲义</item>
             <item >疯狂Ajax讲义</item>
              <item >疯狂XML讲义</item>
        </string-array>
    </resources>

    上面的布局文件中第二个Spinner没有指定android:entries属性,Activity将会采用ArrayAdapter为该Spinner提供列表项目。下面是Activity的代码。

    package org.crazyit.helloworld;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.*;
    
    public class SpinnerTest extends Activity {
       Spinner spinner;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.spinner_test);
            //获取界面布局文件中的Spinner组件
            spinner=(Spinner)findViewById(R.id.spinner);
            String[] arr={"孙悟空","猪八戒","唐僧"};
            //创建ArrayAdapter对象
            ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,
                    arr);
            //为Spinner设置Adapter
            spinner.setAdapter(adapter);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.spinner_test, menu);
            return true;
        }
    
    }

    运行上面的程序将看到如下效果:

  • 相关阅读:
    Ubuntu 18.04.4 系统优化
    Ubuntu 18.04.4 LTS 更换国内系统源
    django 数据库迁移
    django2.0解决跨域问题
    python requests get请求传参
    python 常用排序方法
    python 电脑说话
    centos6.x配置虚拟主机名及域名hosts
    php 合并,拆分,追加,查找,删除数组教程
    PHP统计在线用户数量
  • 原文地址:https://www.cnblogs.com/wolipengbo/p/3379940.html
Copyright © 2011-2022 走看看