Android学习6.2下拉列表二

来源:互联网 发布:程序员算法 编辑:程序博客网 时间:2024/05/17 02:10

xml代码:

<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/cityinfo"
  android:text="选择你喜欢的城市"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>
 <!-- prompt为Spinner组件的提示信息 -->
 <Spinner
     android:id="@+id/mycity"
     android:prompt="@string/city_prompt"
     android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:entries="@array/city_labels"
     />
 <TextView
  android:id="@+id/colorinfo"
  android:text="选择你喜欢的颜色"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>
 <!-- prompt为Spinner组件的提示信息 -->
 <Spinner
     android:id="@+id/mycolor"
     android:layout_width="wrap_content"
  android:layout_height="wrap_content"
     />
 <TextView
  android:id="@+id/eduinfo"
  android:text="请你选择你的学历:"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>
 <!-- prompt为Spinner组件的提示信息 -->
 <Spinner
     android:id="@+id/myedu"
     android:layout_width="wrap_content"
  android:layout_height="wrap_content"
     />
</LinearLayout>

 

java代码:

package cn.chenzhenhua.www.helloworld;

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 MainActivity extends Activity {
 
 private Spinner spiColor=null; // 表示颜色的列表框
 private Spinner spiEdu=null; // 表示学历的列表框
 private ArrayAdapter<CharSequence> adapterColor=null; //下拉列表内容适配器
 private ArrayAdapter<CharSequence> adapterEdu=null;
 
 private List<CharSequence> dataEdu=null;// 集合保存下拉列表选项

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  this.spiColor=(Spinner)super.findViewById(R.id.mycolor);
  this.spiEdu=(Spinner)super.findViewById(R.id.myedu);
  this.spiColor.setPrompt("(java代码实现)请你选择你喜欢的颜色:");
  this.adapterColor=ArrayAdapter.createFromResource(this,
    R.array.color_labels, android.R.layout.simple_spinner_item);// 从资源文件读取信息
  this.adapterColor.setDropDownViewResource(
    android.R.layout.simple_spinner_dropdown_item);// 设置显示风格
  this.spiColor.setAdapter(this.adapterColor);// 设置下拉列表选项
  
  this.dataEdu=new ArrayList<CharSequence>();// 实例化List集合
  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);
  
 }
}

效果图;

 

0 0
原创粉丝点击