内容选择android控件之Spinner(动态生成下拉内容)

本文是一篇关于内容选择的帖子

    main.xml

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

    <LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <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_prompt"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:entries="@array/city_labels"/>

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

    <TextView

        android:id="@+id/info_edu"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="请选择您的学历:" />

    <Spinner

        android:id="@+id/myedu"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" />

    </LinearLayout>

    

    

    strings.xml

    

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MySpinnerDemo!</string>
    <string name="app_name">下拉列表</string>
    <string name="city_prompt">请选择您爱好的都会:</string>

    </resources>

    

    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>

    

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>

    

    每日一道理
只有启程,才会到达理想和目的地,只有拼搏,才会获得辉煌的成功,只有播种,才会有收获。只有追求,才会品味堂堂正正的人。

    MySpinnerDemo.java

    package org.lxh.demo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MySpinnerDemo extends Activity {
    private Spinner spiColor = null; // 表示要读取的颜色列表框
    private Spinner spiEdu = null; // 定义下拉列表
    private ArrayAdapter<CharSequence> adapterColor = null; // 所有的数据都是String
    private ArrayAdapter<CharSequence> adapterEdu = null; // 所有的数据肯定是字符串
    private List<CharSequence> dataEdu = null; // 定义一个集合数据

    @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.color_labels, android.R.layout.simple_spinner_item); // 实例化了ArrayAdapter
        this.adapterColor
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 换个风格
        this.spiColor.setAdapter(this.adapterColor); // 设置表现信息

        // 配置List集合包装的下拉框内容
        this.dataEdu = new ArrayList<CharSequence>();
        this.dataEdu.add("大学");
        this.dataEdu.add("研究生");
        this.dataEdu.add("高中");

        this.spiEdu = (Spinner) super.findViewById(R.id.myedu); // 取得下拉框
        this.spiEdu.setPrompt("请选择您爱好的学历:");
        this.adapterEdu = new ArrayAdapter<CharSequence>(this,
                android.R.layout.simple_spinner_item, this.dataEdu); // 准备好下拉列表框的内容
        this.adapterEdu
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 换个风格
        this.spiEdu.setAdapter(this.adapterEdu);

    //为学历下拉框添加OnItemSelectedListener事件监听器

      this.spiEdu.setOnItemSelectedListener(new OnItemSelectedListener() {
        //选中触发的事件
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            String result=arg0.getItemAtPosition(arg2).toString();
            Log.i("你的学历是:", result);
            
        }

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

    

文章结束给大家分享下程序员的一些笑话语录: 祝大家在以后的日子里. 男生象Oracle般健壮; 女生象win7般漂亮; 桃花运象IE中毒般频繁; 钱包如Gmail容量般壮大, 升职速度赶上微软打补丁 , 追女朋友像木马一样猖獗, 生活像重装电脑后一样幸福, 写程序敲代码和聊天一样有**。

--------------------------------- 原创文章 By
内容和选择
---------------------------------

posted @ 2013-05-24 23:09  坚固66  阅读(498)  评论(0编辑  收藏  举报