Android控件之Spinner探究

来源:互联网 发布:.com域名申请 编辑:程序博客网 时间:2024/05/18 03:50

以下模拟下拉列表的用法

布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout android:id="@+id/LinearLayout01"
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent"
  4.     android:orientation="vertical"
  5.     xmlns:android="http://schemas.android.com/apk/res/android">
  6.     <TextView android:text="@string/ys"
  7.         android:id="@+id/TextView01"
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content"
  10.         android:textSize="28dip" />
  11.     <Spinner android:id="@+id/Spinner01"
  12.         android:layout_width="fill_parent"
  13.         android:layout_height="wrap_content" />
  14. </LinearLayout>
复制代码



SpinnerActivity类

  1. package com.ljq.sp;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.AdapterView;
  8. import android.widget.BaseAdapter;
  9. import android.widget.ImageView;
  10. import android.widget.LinearLayout;
  11. import android.widget.Spinner;
  12. import android.widget.TextView;
  13. import android.widget.AdapterView.OnItemSelectedListener;

  14. public class SpinnerActivity extends Activity {
  15.     private Spinner sp = null;//下拉列表
  16.     private TextView tv = null;
  17.     // 所有资源图片的数组
  18.     private int[] drawableIds={R.drawable.football,R.drawable.basketball,R.drawable.volleyball};
  19.     // 所有字符串的数组
  20.     private int[] msgIds={R.string.zq,R.string.lq,R.string.pq};
  21.    
  22.     @Override
  23.     public void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.main);
  26.         
  27.         tv = (TextView) findViewById(R.id.TextView01);
  28.         sp=(Spinner)this.findViewById(R.id.Spinner01);//初始化Spinner
  29.         sp.setAdapter(adapter);
  30.         sp.setOnItemSelectedListener(new OnItemSelectedListener() {
  31.             public void onItemSelected(AdapterView<?> parent, View view, int positon, long id) {
  32.                 LinearLayout ll = (LinearLayout) view;
  33.                 View v=ll.getChildAt(0);//获取第一个控件ImageView
  34.                 Log.i("ljq", v.getClass().getName());
  35.                 TextView tvn = (TextView) ll.getChildAt(1);//获取第二个控件TextView
  36.                 StringBuilder sb = new StringBuilder();
  37.                 sb.append(getResources().getText(R.string.ys)).append(":").append(tvn.getText());
  38.                 tv.setText(sb.toString());
  39.             }

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

  41.             }
  42.         });
  43.     }
  44.    
  45.     private BaseAdapter adapter = new BaseAdapter(){
  46.         public int getCount() {
  47.             return drawableIds.length;
  48.         }
  49.         
  50.         public Object getItem(int position) {
  51.             return drawableIds[position];
  52.         }
  53.         
  54.         public long getItemId(int position) {
  55.             return position;
  56.         }
  57.         
  58.         public View getView(int position, View convertView, ViewGroup parent) {
  59.             LinearLayout ll = new LinearLayout(SpinnerActivity.this);
  60.             ll.setOrientation(LinearLayout.HORIZONTAL);
  61.             
  62.             ImageView iv = new ImageView(SpinnerActivity.this);
  63.             iv.setImageResource(drawableIds[position]);
  64.             ll.addView(iv);
  65.             
  66.             TextView tv=new TextView(SpinnerActivity.this);
  67.             tv.setText(msgIds[position]);//设置内容
  68.             tv.setTextSize(24);
  69.             tv.setTextColor(R.color.black);
  70.             ll.addView(tv);
  71.             return ll;
  72.         }
  73.     };
  74. }
复制代码



运行结果

1.png

2011-5-14 09:01:04 上传
下载附件(33.64 KB)


分类: android常用控件
标签: android Spinner, android 下拉列表
原创粉丝点击