Android 之 下拉框(Spinner)的使用
来源:互联网 发布:开源数据交换平台架构 编辑:程序博客网 时间:2024/06/06 17:57
转自:http://www.oschina.net/question/54100_33274
出处:http://imshare.iteye.com/blog/770950
下拉列表 Spinner。
Spinner的使用,可以极大提高用户的体验性。当需要用户选择的时候,可以提供一个下拉列表将所有可选的项列出来。供用户选择。
Demo如下,可以留作参考
一、使用数组作为数据源
1.新建一个android的工程
2.工程的layout.xml文件如下:声明一个TextView控件和一个Spinner控件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:orientation
=
"vertical"
>
<
TextView
android:id
=
"@+id/spinnerText"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
></
TextView
>
<
Spinner
android:id
=
"@+id/Spinner01"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
></
Spinner
>
</
LinearLayout
>
3.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
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
static
final
String[] m={
"A型"
,
"B型"
,
"O型"
,
"AB型"
,
"其他"
};
private
TextView view ;
private
Spinner spinner;
private
ArrayAdapter<String> adapter;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
view = (TextView) findViewById(R.id.spinnerText);
spinner = (Spinner) findViewById(R.id.Spinner01);
//将可选内容与ArrayAdapter连接起来
adapter =
new
ArrayAdapter<String>(
this
,android.R.layout.simple_spinner_item,m);
//设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将adapter 添加到spinner中
spinner.setAdapter(adapter);
//添加事件Spinner事件监听
spinner.setOnItemSelectedListener(
new
SpinnerSelectedListener());
//设置默认值
spinner.setVisibility(View.VISIBLE);
}
//使用数组形式操作
class
SpinnerSelectedListener
implements
OnItemSelectedListener{
public
void
onItemSelected(AdapterView<?> arg0, View arg1,
int
arg2,
long
arg3) {
view.setText(
"你的血型是:"
+m[arg2]);
}
public
void
onNothingSelected(AdapterView<?> arg0) {
}
}
}
4、运行效果如下:
二、使用XML作为数据源
1.新建一个android的工程
2.在values文件夹下新建一个arryas.xml文件:声明一个TextView控件和一个Spinner控件,代码如下:
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
>
3.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
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) {
}
}
}
4、运行效果如下:
0 0
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- Android 之 下拉框(Spinner)的使用
- 如何在quicksettings中增加一个新的buttons
- 【转】Android TextView支持拷贝&粘帖 textIsSelectable
- 杭电ACM 1012 u Calculate e java
- Unity3D 学习笔记3——了解U3D引擎的操作面板和各种工具
- 设置popWindow背景变暗
- Android 之 下拉框(Spinner)的使用
- android中WebView的自适应
- android获取当前日期及具体值
- WebView.loadUrl方法弹出外部浏览器的解决办法
- MATLAB杂记
- android开发could not find class解决方案
- eclipse快捷键失效
- android退出app的四种方法
- Ubuntu下嵌入式Qt开发环境配置全攻略