今天课上学习了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 }
效果截图
备注:开发环境为ADT