本文实例讲述了android编程开发之spinner控件用法。分享给大家供大家参考,具体如下:
下拉列表 spinner,spinner是一个每次只能选择所有项的一个项的控件。它的项来自于与之相关联的适配器中。
spinner的使用,可以极大提高用户的体验性。当需要用户选择的时候,可以提供一个下拉列表将所有可选的项列出来。供用户选择。
一、使用数组作为数据源
布局文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= ".mainactivity" > <spinner android:id= "@+id/spinner1" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" /> </relativelayout> |
mainactivity.java
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
|
package com.example.lesson16_spinner; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.simpleadapter; import android.widget.spinner; import android.widget.toast; public class mainactivity extends activity { // 声明spinner对象 private spinner spinner; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); spinner = (spinner) findviewbyid(r.id.spinner1); //使用数组作为数据源 final string arr[] = new string[] { "周一" , "周二" , "周三" , "周四" , "周五" , "周六" , "周七" }; // adpater对象 arrayadapter<string> arrayadapter = new arrayadapter<string>( this , android.r.layout.simple_spinner_item, arr); list<map<string, object>> data = new arraylist<map<string, object>>(); map<string, object> map = new hashmap<string, object>(); map.put( "logo" , r.drawable.ic_launcher); map.put( "text" , "xxxx说明操作" ); map<string, object> map2 = new hashmap<string, object>(); map2.put( "logo" , r.drawable.ic_launcher); map2.put( "text" , "xxx说明xxx" ); data.add(map); data.add(map2); simpleadapter simpleadapter = new simpleadapter( this , data, r.layout.activity_item, new string[] { "logo" , "text" }, new int [] { r.id.imageview1, r.id.textview1 }); // 设置显示的数据 spinner.setadapter(arrayadapter); //spinner.setadapter(simpleadapter); toast.maketext(getapplicationcontext(), "main thread" + spinner.getitematposition(spinner.getselecteditemposition()), toast.length_long).show(); // 注册事件 spinner.setonitemselectedlistener( new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { // string content = arr[position]; spinner spinner = (spinner) parent; toast.maketext(getapplicationcontext(), "xxxx" + spinner.getitematposition(position), toast.length_long).show(); } @override public void onnothingselected(adapterview<?> parent) { toast.maketext(getapplicationcontext(), "没有改变的处理" , toast.length_long).show(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true ; } } |
效果如下:
二、使用xml作为数据源(非本人代码)
布局文件:
1
2
3
4
5
6
7
8
9
10
|
<?xml version= "1.0" encoding= "utf-8" ?> <resources> <string-array name= "plantes" > <item>nokia</item> <item>moto</item> <item>htc</item> <item>lg</item> <item>其他</item> </string-array> </resources> |
java代码:
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
|
import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemselectedlistener; import android.widget.arrayadapter; import android.widget.spinner; import android.widget.textview; public class spinneractivity extends activity { private textview view2; private spinner spinner2; private arrayadapter adapter2; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super .oncreate(savedinstancestate); setcontentview(r.layout.spinner); spinner2 = (spinner) findviewbyid(r.id.spinner02); view2 = (textview) findviewbyid(r.id.spinnertext02); //将可选内容与arrayadapter连接起来 adapter2 = arrayadapter.createfromresource( this , r.array.plantes, android.r.layout.simple_spinner_item); //设置下拉列表的风格 adapter2.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); //将adapter2 添加到spinner中 spinner2.setadapter(adapter2); //添加事件spinner事件监听 spinner2.setonitemselectedlistener( new spinnerxmlselectedlistener()); //设置默认值 spinner2.setvisibility(view.visible); } //使用xml形式操作 class spinnerxmlselectedlistener implements onitemselectedlistener{ public void onitemselected(adapterview<?> arg0, view arg1, int arg2, long arg3) { view2.settext( "你使用什么样的手机:" +adapter2.getitem(arg2)); } public void onnothingselected(adapterview<?> arg0) { } } } |
希望本文所述对大家android程序设计有所帮助。