Android ApiDemos示例解析(192):Views->Spinner
Spinner为下拉框,为AdapterView的子类。AdapterView的显示可以通过数据绑定来实现,数据源可以是数组或是数据库记录,数据源和AdapterView是通过Adapter作为桥梁。通过Adapter,AdatperView可以显示数据源或处理用户选取事件,如:选择列表中某项。
本例Adapter 使用的是ArrayAdapter,可以参见Android ApiDemos示例解析(168):Views->Lists->1. Array
并为Spinner 添加事件处理:
[java] view plaincopyprint?
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter
= ArrayAdapter.createFromResource(
this, R.array.colors,
android.R.layout.易做图_spinner_item);
adapter.setDropDownViewResource(android.R
.layout.易做图_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");
}
});
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter
= ArrayAdapter.createFromResource(
this, R.array.colors,
android.R.layout.易做图_spinner_item);
adapter.setDropDownViewResource(android.R
.layout.易做图_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 ,