Android控件之Spinner用法实例分析

时间:2021-11-30 06:19:33

本文实例讲述了android控件之spinner用法。分享给大家供大家参考。具体如下:

以下模拟下拉列表的用法

布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<linearlayout android:id="@+id/linearlayout01"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 android:orientation="vertical"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <textview android:text="@string/ys"
 android:id="@+id/textview01"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:textsize="28dip" />
 <spinner android:id="@+id/spinner01"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" />
</linearlayout>

spinneractivity类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.ljq.sp;
import android.app.activity;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.view.viewgroup;
import android.widget.adapterview;
import android.widget.baseadapter;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.spinner;
import android.widget.textview;
import android.widget.adapterview.onitemselectedlistener;
public class spinneractivity extends activity {
 private spinner sp = null;//下拉列表
 private textview tv = null;
 // 所有资源图片的数组
 private int[] drawableids={r.drawable.football,r.drawable.basketball,r.drawable.volleyball};
 // 所有字符串的数组
 private int[] msgids={r.string.zq,r.string.lq,r.string.pq};
 @override
 public void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.main);
 tv = (textview) findviewbyid(r.id.textview01);
 sp=(spinner)this.findviewbyid(r.id.spinner01);//初始化spinner
 sp.setadapter(adapter);
 sp.setonitemselectedlistener(new onitemselectedlistener() {
  public void onitemselected(adapterview<?> parent, view view, int positon, long id) {
  linearlayout ll = (linearlayout) view;
  view v=ll.getchildat(0);//获取第一个控件imageview
  log.i("ljq", v.getclass().getname());
  textview tvn = (textview) ll.getchildat(1);//获取第二个控件textview
  stringbuilder sb = new stringbuilder();
  sb.append(getresources().gettext(r.string.ys)).append(":").append(tvn.gettext());
  tv.settext(sb.tostring());
  }
  public void onnothingselected(adapterview<?> parent) {
  }
 });
 }
 private baseadapter adapter = new baseadapter(){
 public int getcount() {
  return drawableids.length;
 }
 public object getitem(int position) {
  return drawableids[position];
 }
 public long getitemid(int position) {
  return position;
 }
 public view getview(int position, view convertview, viewgroup parent) {
  linearlayout ll = new linearlayout(spinneractivity.this);
  ll.setorientation(linearlayout.horizontal);
  imageview iv = new imageview(spinneractivity.this);
  iv.setimageresource(drawableids[position]);
  ll.addview(iv);
  textview tv=new textview(spinneractivity.this);
  tv.settext(msgids[position]);//设置内容
  tv.settextsize(24);
  tv.settextcolor(r.color.black);
  ll.addview(tv);
  return ll;
 }
 };
}

运行结果

Android控件之Spinner用法实例分析

希望本文所述对大家的android序设计有所帮助。