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
62
|
public
class
MainActivity
extends
Activity
{
/**
* 1.XML文件配置:
* 添加弹出对话框的标题: android:prompt="@string/spinnername1"
* 2.Adapter的布局:android.R.layout.simple_spinner_item
* resource设置结果显示框的布局:new ArrayAdapter(context, resource, textViewResourceId)
* 3.点击事件:s1.setOnItemSelectedListener
* 4.设置弹出对话框的行布局:
* adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
*
* 5.得到资源文件:Resources res = getResources()
* 6.通过资源id获取资源文件的内容:String[] arrStr = res.getStringArray(R.array.colors)
*
*/
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Resources res = getResources();
String[] arrStr = res.getStringArray(R.array.colors);
ArrayAdapter<String> adapter =
new
ArrayAdapter<String>(
this
, R.layout.spinnertextview, arrStr);
/**
* 1.APIdemo的原写法:
* ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.colors, android.R.layout.simple_spinner_item);
* 2.ArrayAdapter.createFromResource
* 此方法将R.array.colors这个字符串数组文件的内容得到(通过id得到资源,通过方法得到内容)
*/
adapter.setDropDownViewResource(R.layout.spinnercheckeditem);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(
new
OnItemSelectedListener()
{
@Override
public
void
onItemSelected(AdapterView<?> parent, View view,
int
position,
long
id)
{
Toast.makeText(MainActivity.
this
,
"Spinner1: position="
+ position +
" id="
+ id, Toast.LENGTH_SHORT).show();
}
@Override
public
void
onNothingSelected(AdapterView<?> parent)
{
Toast.makeText(MainActivity.
this
,
"Spinner1: unselected"
, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1203966,如需转载请自行联系原作者