欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发之Spinner

程序员文章站 2022-07-02 08:16:50
今天课上学习了Spinner,做了一个很简单的程序,现将代码和截图放在这里供大家参考。 activity_spinner.xml 1 2

今天课上学习了spinner,做了一个很简单的程序,现将代码和截图放在这里供大家参考。

activity_spinner.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="horizontal" 
 7     tools:context=".mainactivity">
 8     
 9     <spinner
10         android:id="@+id/spinner1"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:layout_margintop="5dp"
14         android:layout_weight="1" />
15 
16     <spinner
17         android:id="@+id/spinner2"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:layout_margintop="5dp"
21         android:layout_weight="1" />
22     
23     <spinner
24         android:id="@+id/spinner3"
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:layout_margintop="5dp"
28         android:layout_weight="1" />
29     
30 </linearlayout>

mainactivity.java

  1 package com.example.spinner;
  2 
  3 import android.os.bundle;
  4 import android.app.activity;
  5 import android.view.menu;
  6 import android.view.view;
  7 import android.widget.adapterview;
  8 import android.widget.adapterview.onitemselectedlistener;
  9 import android.widget.arrayadapter;
 10 import android.widget.spinner;
 11 import android.widget.textview;
 12 import android.widget.toast;
 13 
 14 public class mainactivity extends activity {
 15 
 16     private stringbuffer stringbuffer;
 17     protected string str3;
 18     protected string str2;
 19     protected string str1;
 20    
 21 
 22     @override
 23     protected void oncreate(bundle savedinstancestate) {
 24         super.oncreate(savedinstancestate);
 25         setcontentview(r.layout.activity_spinner);
 26         
 27         stringbuffer = new stringbuffer();
 28         
 29         //获取界面布局文件中的spinner组件
 30         spinner spinner1 = (spinner) findviewbyid(r.id.spinner1);
 31         spinner spinner2 = (spinner) findviewbyid(r.id.spinner2);
 32         spinner spinner3 = (spinner) findviewbyid(r.id.spinner3);
 33         
 34         //字符串数组
 35         string[] stringarr1 = {"机器人学院","智慧城市学院","特教学院","城市轨道交通学院"};
 36         string[] stringarr2 = {"软件工程","机械工程","机器人工程","人工智能"};
 37         string[] stringarr3 = {"2016级","2017级","2018级","2019级"};
 38         
 39         //创建arrayadapter对象
 40         arrayadapter<string> adapter1 = new arrayadapter(mainactivity.this,android.r.layout.simple_spinner_item,stringarr1);
 41         arrayadapter<string> adapter2 = new arrayadapter(mainactivity.this,android.r.layout.simple_spinner_item,stringarr2);
 42         arrayadapter<string> adapter3 = new arrayadapter(mainactivity.this,android.r.layout.simple_spinner_item,stringarr3);
 43         
 44         //为spinner1设置adapter
 45         spinner1.setadapter(adapter1);
 46         
 47         //为spinner1设置选中事件监听器
 48         spinner1.setonitemselectedlistener(new onitemselectedlistener(){
 49 
 50             @override
 51             public void onitemselected(adapterview<?> parent, view view,
 52                     int position, long id) {
 53                 // todo auto-generated method stub
 54                 
 55                 textview tv = (textview) view;
 56                 str1 = tv.gettext().tostring();
 57                 add();
 58                 
 59             }
 60 
 61             @override
 62             public void onnothingselected(adapterview<?> parent) {
 63                 // todo auto-generated method stub
 64                 
 65             }
 66             
 67         } );
 68         
 69         //为spinner2设置adapter
 70         spinner2.setadapter(adapter2);
 71         
 72         //为spinner2设置选中事件监听器
 73         spinner2.setonitemselectedlistener(new onitemselectedlistener(){
 74 
 75             @override
 76             public void onitemselected(adapterview<?> parent, view view,
 77                     int position, long id) {
 78                 // todo auto-generated method stub
 79                 
 80                 textview tv = (textview) view;
 81                 str2 = tv.gettext().tostring();
 82                 add();
 83             }
 84 
 85             @override
 86             public void onnothingselected(adapterview<?> parent) {
 87                 // todo auto-generated method stub
 88                 
 89             }
 90             
 91         });
 92         
 93         //为spinner3设置adapter
 94         spinner3.setadapter(adapter3);
 95         
 96         //为spinner3设置选中事件监听器
 97         spinner3.setonitemselectedlistener(new onitemselectedlistener(){
 98 
 99             @override
100             public void onitemselected(adapterview<?> parent, view view,
101                     int position, long id) {
102                 // todo auto-generated method stub
103                 
104                 textview tv = (textview) view;
105                 str3 = tv.gettext().tostring();
106                 add();
107                  
108             }
109 
110             @override
111             public void onnothingselected(adapterview<?> parent) {
112                 // todo auto-generated method stub
113             
114             }
115             
116         });
117         
118     }
119 
120 
121     protected void add() {
122         // todo auto-generated method stub
123         stringbuffer.delete(0, stringbuffer.length());
124         show(str1,str2,str3);
125         toast.maketext(mainactivity.this, stringbuffer, toast.length_long)
126           .show();
127     }
128 
129 
130     private void show(string str1, string str2, string str3) {
131         // todo auto-generated method stub
132         stringbuffer.append(str1).append("--");
133         stringbuffer.append(str2).append("--");
134         stringbuffer.append(str3);
135     }
136 
137 
138     @override
139     public boolean oncreateoptionsmenu(menu menu) {
140         // inflate the menu; this adds items to the action bar if it is present.
141         getmenuinflater().inflate(r.menu.main, menu);
142         return true;
143     }
144     
145 }

效果截图

Android开发之Spinner

Android开发之Spinner

备注:开发环境为adt