Android常用高级组件之下拉列表控件

1.Spinner类简介

   Spinner位于android:widget包下,每次只显示用户选中的元素,当用户再次点击是,会弹出选择列表供用户选择而选择列表中的元素同样来自适配器,它的继承关系如下:

Java.lang.Object/Adroid.view.View/android.ViewGoup/android.widget.AdapterView/android.widget.AbsSpinner/android.widget.Spinner

2下拉列表使用案例

a.创建项目,并准备图片资源,将图片资源存放到项目目录中的res/drawable-mdpi文件夹下;

b.准备字符串资源;

<resources>
    <string name="app_name">Sample_5_9</string>
    <string name="hello">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="ys">您的爱好</string>
    <string name="lq">篮球</string>
    <string name="zq">足球</string>
    <string name="pq">排球</string>
</resources>

c.准备颜色资源,在res/values目录下创建color.xml文件,并写入代码;

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#fd8d8d</color>
    <color name="green">#9cfda3</color>
    <color name="blue">#8d9dfd</color>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
</resources>

d.开发该案例的布局文件,即main.xml,如下;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout01"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/ys"
        android:id="@+id/TextView01"
        android:textSize="28dip" />
    <Spinner 
        android:id="@+id/Spinner01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

e.开发案例的主要逻辑代码,如下:

package com.example.sample_5_9;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class Sample_5_9 extends Activity 
{
    final static int WRAP_CONTENT = -2;//表示WRAP_CONTETN    的常量
    int[] drawableIds = {R.drawable.football, R.drawable.basketball, R.drawable.volleyball};//所有资源文件
    int[] msgIds = {R.string.zq, R.string.lq,R.string.pq};//所有资源字符串
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Spinner sp = (Spinner)this.findViewById(R.id.Spinner01);//初始化Spinner
        BaseAdapter ba = new BaseAdapter()
        {
            public int getCount()
            {
                return 3;
            }
            public Object getItem(int position)
            {
                return null;
            }
            public long getItemId(int position)
            {
                return 0;
            }
            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {//动态生成每个下拉项对应的View,每个下拉项View由LinearLayout中包含一个ImageView及一个TextView构成
                LinearLayout ll = new LinearLayout(Sample_5_9.this);
                ll.setOrientation(LinearLayout.HORIZONTAL);//设置朝向
                ImageView ii = new ImageView(Sample_5_9.this);
                ii.setImageDrawable(getResources().getDrawable(drawableIds[position]));//设置图片
                ll.addView(ii);//将ImageView添加到LinearLayout中
                TextView tv = new TextView(Sample_5_9.this);
                tv.setText(" "+ getResources().getText(msgIds[position]));//设置内容
                tv.setTextSize(24);
                tv.setTextColor(R.color.black);
                ll.addView(tv);
                return ll;
            }
        };
        sp.setAdapter(ba);
       sp.setOnItemSelectedListener(
               new OnItemSelectedListener()
            {
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3)
                {
                    TextView tv = (TextView)findViewById(R.id.TextView01);
                    LinearLayout ll = (LinearLayout)arg1;//获取当前选中选项对应的LinearLayout
                    TextView tvn = (TextView)ll.getChildAt(1);//获取其中的TextView
                    StringBuilder sb = new StringBuilder();//用StringBuilder动态生成信息
                    sb.append(getResources().getText(R.string.ys));
                    sb.append(":");
                    sb.append(tvn.getText());
                    tv.setText(sb.toString());
                    
                }
                public void onNothingSelected(AdapterView<?> arg0)
                {
                }
            });
    }
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

f.程序运行结果如下:

转载于:https://www.cnblogs.com/yujiaqiang/archive/2012/09/20/2694673.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值