免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2052 | 回复: 0
打印 上一主题 下一主题

Android控件之Spinner探究 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-25 16:38 |只看该作者 |倒序浏览
Android控件之Spinner探究



以下模拟下拉列表的用法

布局文件
  1. 01.<?xml version="1.0" encoding="utf-8"?>

  2. 02.<LinearLayout android:id="@+id/LinearLayout01"

  3. 03.    android:layout_width="fill_parent" android:layout_height="fill_parent"

  4. 04.    android:orientation="vertical"

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

  6. 06.    <TextView android:text="@string/ys"

  7. 07.        android:id="@+id/TextView01"

  8. 08.        android:layout_width="fill_parent"

  9. 09.        android:layout_height="wrap_content"

  10. 10.        android:textSize="28dip" />

  11. 11.    <Spinner android:id="@+id/Spinner01"

  12. 12.        android:layout_width="fill_parent"

  13. 13.        android:layout_height="wrap_content" />

  14. 14.</LinearLayout>
复制代码
复制代码



SpinnerActivity类
  1. 01.package com.ljq.sp;

  2. 02.

  3. 03.import android.app.Activity;

  4. 04.import android.os.Bundle;

  5. 05.import android.util.Log;

  6. 06.import android.view.View;

  7. 07.import android.view.ViewGroup;

  8. 08.import android.widget.AdapterView;

  9. 09.import android.widget.BaseAdapter;

  10. 10.import android.widget.ImageView;

  11. 11.import android.widget.LinearLayout;

  12. 12.import android.widget.Spinner;

  13. 13.import android.widget.TextView;

  14. 14.import android.widget.AdapterView.OnItemSelectedListener;

  15. 15.

  16. 16.public class SpinnerActivity extends Activity {

  17. 17.    private Spinner sp = null;//下拉列表

  18. 18.    private TextView tv = null;

  19. 19.    // 所有资源图片的数组

  20. 20.    private int[] drawableIds={R.drawable.football,R.drawable.basketball,R.drawable.volleyball};

  21. 21.    // 所有字符串的数组

  22. 22.    private int[] msgIds={R.string.zq,R.string.lq,R.string.pq};

  23. 23.   

  24. 24.    @Override

  25. 25.    public void onCreate(Bundle savedInstanceState) {

  26. 26.        super.onCreate(savedInstanceState);

  27. 27.        setContentView(R.layout.main);

  28. 28.        

  29. 29.        tv = (TextView) findViewById(R.id.TextView01);

  30. 30.        sp=(Spinner)this.findViewById(R.id.Spinner01);//初始化Spinner

  31. 31.        sp.setAdapter(adapter);

  32. 32.        sp.setOnItemSelectedListener(new OnItemSelectedListener() {

  33. 33.            public void onItemSelected(AdapterView<?> parent, View view, int positon, long id) {

  34. 34.                LinearLayout ll = (LinearLayout) view;

  35. 35.                View v=ll.getChildAt(0);//获取第一个控件ImageView

  36. 36.                Log.i("ljq", v.getClass().getName());

  37. 37.                TextView tvn = (TextView) ll.getChildAt(1);//获取第二个控件TextView

  38. 38.                StringBuilder sb = new StringBuilder();

  39. 39.                sb.append(getResources().getText(R.string.ys)).append(":").append(tvn.getText());

  40. 40.                tv.setText(sb.toString());

  41. 41.            }

  42. 42.

  43. 43.            public void onNothingSelected(AdapterView<?> parent) {

  44. 44.

  45. 45.            }

  46. 46.        });

  47. 47.    }

  48. 48.   

  49. 49.    private BaseAdapter adapter = new BaseAdapter(){

  50. 50.        public int getCount() {

  51. 51.            return drawableIds.length;

  52. 52.        }

  53. 53.        

  54. 54.        public Object getItem(int position) {

  55. 55.            return drawableIds[position];

  56. 56.        }

  57. 57.        

  58. 58.        public long getItemId(int position) {

  59. 59.            return position;

  60. 60.        }

  61. 61.        

  62. 62.        public View getView(int position, View convertView, ViewGroup parent) {

  63. 63.            LinearLayout ll = new LinearLayout(SpinnerActivity.this);

  64. 64.            ll.setOrientation(LinearLayout.HORIZONTAL);

  65. 65.            

  66. 66.            ImageView iv = new ImageView(SpinnerActivity.this);

  67. 67.            iv.setImageResource(drawableIds[position]);

  68. 68.            ll.addView(iv);

  69. 69.            

  70. 70.            TextView tv=new TextView(SpinnerActivity.this);

  71. 71.            tv.setText(msgIds[position]);//设置内容

  72. 72.            tv.setTextSize(24);

  73. 73.            tv.setTextColor(R.color.black);

  74. 74.            ll.addView(tv);

  75. 75.            return ll;

  76. 76.        }

  77. 77.    };

  78. 78.}
复制代码
复制代码



运行结果



分类: android常用控件
标签: android Spinner, android 下拉列表
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP