Android ApiDemos示例解析(192):Views->Spinner
程序员文章站
2022-03-30 10:14:34
...
Spinner为下拉框,为AdapterView的子类。AdapterView的显示可以通过数据绑定来实现,数据源可以是数组或是数据库记录,数据源和AdapterView是通过Adapter作为桥梁。通过Adapter,AdatperView可以显示数据源或处理用户选取事件,如:选择列表中某项。
本例Adapter 使用的是ArrayAdapter,可以参见Android ApiDemos示例解析(168):Views->Lists->1. Array
并为Spinner 添加事件处理:
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter
= ArrayAdapter.createFromResource(
this, R.array.colors,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R
.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent,
View view, int position, long id) {
showToast("Spinner1: position="
+ position + " id=" + id);
}
public void onNothingSelected(AdapterView<?> parent) {
showToast("Spinner1: unselected");
}
});
推荐阅读
-
Android ApiDemos示例解析(19):App->Alarm->Alarm Controller
-
Android ApiDemos示例解析(19):App->Alarm->Alarm Controller
-
Android ApiDemos示例解析(20):App->Alarm->Alarm Service
-
Android ApiDemos示例解析(84):Graphics->UnicodeChart
-
Android ApiDemos示例解析(83):Graphics->Typefaces
-
Android ApiDemos示例解析(83):Graphics->Typefaces
-
Android ApiDemos示例解析(85):Graphics->Vertices
-
Android ApiDemos示例解析(21):App->Device Admin
-
Android ApiDemos示例解析(21):App->Device Admin
-
Android ApiDemos示例解析(84):Graphics->UnicodeChart