Android编程开发之Spinner组件用法
程序员文章站
2023-12-16 14:39:04
本文实例讲述了android编程开发之spinner组件用法。分享给大家供大家参考,具体如下:
spinner组件组要用显示一个下拉列表,在使用中需要用到适配器adapt...
本文实例讲述了android编程开发之spinner组件用法。分享给大家供大家参考,具体如下:
spinner组件组要用显示一个下拉列表,在使用中需要用到适配器adapter,下面是一个该组件的使用示例
首先是布局文件main.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <spinner android:id="@+id/spinner1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <spinner android:id="@+id/spinner2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margintop="20dp"/> </linearlayout>
由于用到simpadapter所以要写子项item的布局如下 item.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <imageview android:id="@+id/ivlogo" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/icon" android:paddingleft="10dp" /> <textview android:id="@+id/tvapplicationname" android:textcolor="#000" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textsize="16dp" android:gravity="center_vertical" android:paddingleft="10dp" /> </linearlayout>
下面是代码:
import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import android.app.activity; import android.app.alertdialog; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.simpleadapter; import android.widget.spinner; import android.widget.adapterview.onitemselectedlistener; public class main extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); //获取对象 spinner spinner1 = (spinner) findviewbyid(r.id.spinner1); string[] applicationnames = new string[] { "多功能日历", "eoemarket客户端", "耐玩的重力消砖块", "白社会", "程序终结者" }; arrayadapter<string> aaadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, applicationnames); // 将如下代码可以使列表项带radiobutton组件 // aaadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner1.setadapter(aaadapter); spinner spinner2 = (spinner) findviewbyid(r.id.spinner2); final list<map<string, object>> items = new arraylist<map<string, object>>(); map<string, object> item1 = new hashmap<string, object>(); item1.put("ivlogo", r.drawable.calendar); item1.put("tvapplicationname", "多功能日历"); map<string, object> item2 = new hashmap<string, object>(); item2.put("ivlogo", r.drawable.eoemarket); item2.put("tvapplicationname", "eoemarket客户端"); items.add(item1); items.add(item2); simpleadapter simpleadapter = new simpleadapter(this, items, r.layout.item, new string[] { "ivlogo", "tvapplicationname" }, new int[] { r.id.ivlogo, r.id.tvapplicationname }); spinner2.setadapter(simpleadapter); //为spinner2加上监听事件 spinner2.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { new alertdialog.builder(view.getcontext()).settitle( items.get(position).get("tvapplicationname") .tostring()).seticon( integer.parseint(items.get(position).get("ivlogo") .tostring())).show(); } @override public void onnothingselected(adapterview<?> parent) { } }); } }
希望本文所述对大家android程序设计有所帮助。