Android控件之Spinner用法实例分析
程序员文章站
2023-11-25 10:05:16
本文实例讲述了android控件之spinner用法。分享给大家供大家参考。具体如下:
以下模拟下拉列表的用法
布局文件:
本文实例讲述了android控件之spinner用法。分享给大家供大家参考。具体如下:
以下模拟下拉列表的用法
布局文件:
<?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类:
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序设计有所帮助。
上一篇: Android控件之ImageView用法实例分析
下一篇: C#中事件的定义和使用