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
            
        }
    });
    }
}




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值