Spinner: 下拉组件

使用事项:布局在XML 中实现,具体的数据在JAVA 代码中实现;

所用知识点:
数组适配器:ArrayAdapter 用于关系M 层和 C 层;
事件:OnItemSelectedListener;

案列:查看十二星座效果图:

xml:代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ScrollView        android:layout_width="wrap_content"        android:layout_height="wrap_content" >        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:gravity="center_horizontal"            android:orientation="vertical" >            <Spinner                android:id="@+id/spinner"                android:layout_width="300dp"                android:layout_height="wrap_content"                android:layout_gravity="center_vertical" />            <TextView                android:id="@+id/showInfo"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_marginLeft="15dp"                android:layout_marginRight="15dp"                android:gravity="center"                android:text=" " />        </LinearLayout>    </ScrollView></LinearLayout>


java代码如下:
package com.example.spinnertest;import java.util.ArrayList;import android.app.Activity;import android.content.res.Resources;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.TextView;public class MainActivity extends Activity {private Spinner spinner;private TextView tx;private ArrayList<String> list = null;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);/* 获取TextView 实体对象  , 用于显示星座的详细信息  */tx = (TextView) findViewById(R.id.showInfo);/* 下拉组件测试 Spinner 对象首先获取 */spinner = (Spinner) findViewById(R.id.spinner);/* 准备数据源 M , 用集合进行保存  */list = new ArrayList<String>(); list.add("Aries");list.add("Taurus");list.add("Gemini");list.add("Cancer");list.add("Leo");list.add("Virgo");list.add("Libra");list.add("Scorpio");list.add("Sagittarius");list.add("Capricorn");list.add("Aquarius");list.add("Pisces");/* 实现M 层 与C 层的关系 ,绑定数据  */ /* 参数1:上下文对象; 参数2:系统资源布局方式 ; 参数3:数据对象 */ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);/* 对V 层和C 层进行关系的绑定;  */spinner.setAdapter(adapter);spinner.setOnItemSelectedListener(new OnItemSelectedListener() {/* 对所有的选择项进行监控  *//* 参3:当前选择项的ID  参4:被选择项在组中的位置,逻辑上与ID 相等,但代表的意义不一样  */public void onItemSelected(AdapterView<?> arg0, View arg1,int  id, long position) {//Toast.makeText(MainActivity.this, "你选择的是第:"+id+"值为:"+list.get(id), 1000).show();/* 设置tx对象的值  */String temp = getConstellation(id);tx.setText("\t"+temp);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});}/* 显示星座的信息 */protected String getConstellation(int id){Resources rs  = getResources();String temp = "";switch(id){case 0:temp = rs.getString(R.string.Aries);break;case 1:temp = rs.getString(R.string.Taurus);break;case 2:temp = rs.getString(R.string.Gemini);break;case 3:temp = rs.getString(R.string.Cancer);break;case 4:temp = rs.getString(R.string.Leo);break;case 5:temp = rs.getString(R.string.Virgo);break;case 6:temp = rs.getString(R.string.Libra);break;case 7:temp = rs.getString(R.string.Scorpio);break;case 8:temp = rs.getString(R.string.Sagittarius);break;case 9:temp = rs.getString(R.string.Capricorn);break;case 10:temp = rs.getString(R.string.Aquarius);break;case 11:temp = rs.getString(R.string.Pisces);break;case 12:temp = rs.getString(R.string.Aries);break;}return temp;}}


更多相关文章

  1. Android来电号码获取代码
  2. android 之对象传递
  3. 构建自己的Android代码托管服务器
  4. Android中Intent对象与Intent Filter过滤匹配过程详解
  5. Android 2.2中的APK安装参数installLocation
  6. 第一行代码Android(一)
  7. Android 学习日记(二)android studio运行github代码
  8. Android Apk反编译得到Java源代码
  9. Android 通过java代码实现EditText输入限制

随机推荐

  1. eclipse创建android项目,无法正常预览布局
  2. android之ION内存管理器(1)-- 简介
  3. android:ellipsize
  4. [置顶] Android编译系统详解(一)
  5. Android让所有应用都竖屏显示或者横屏显
  6. unity 与 android 协调工作 注意事项
  7. android基础(开发环境搭建)
  8. 在 Android(安卓)Studio 上调试数据库 (
  9. Android Studio 启用Lambda表达式
  10. android SDK编译版本修改