Android学习笔记_72_Spinner的用法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Android学习笔记_72_Spinner的⽤法⼀、普通
1、
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="left"
android:paddingLeft="8dip"
android:singleLine="true"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="@color/gsdk_black"
android:text="sdf"/>
2
private String[] method = null;
ArrayAdapter<String> methodAdapter = new ArrayAdapter<String>(this,yout.simple_spinner_item, method); methodSpinner = (Spinner) findViewById(R.id.spinner_recharge_type);
moneyAdapter
.setDropDownViewResource(yout.select_dialog_item);
methodSpinner.setAdapter(methodAdapter);
⼆、⾃定义spinner:
1、配置⽂件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_spinner_id"
android:layout_width="60dp"
android:layout_height="80dp"
android:visibility="gone"/>
<TextView
android:id="@+id/tv_spinner_name"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="35dip"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textColor="#000"
android:textSize="@dimen/font_body"/>
</LinearLayout>
2、填充数据
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
for (Project project : linkedList) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", project.getName());
map.put("id", project.getId());
data.add(map);
}
searchAdapter = new SimpleAdapter(PurchaseActivity.this, data,
yout.purchase_search_spinner, new String[]{"id","name"},
new int[]{_spinner_id,_spinner_name});
searchSpinner.setAdapter(searchAdapter);
3、点击事件:注意点击事件是OnItemSelectedListener⽽不是OnItemClickListener.
private class SearchSpinnerItemListener implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Spinner spinner = (Spinner) parent;
Map<String, Object> projectMap = (Map<String, Object>) spinner.getItemAtPosition(position);
int pid = Integer.valueOf(projectMap.get("id")+"");
String pname = (String) projectMap.get("name");
Log.i("Purchase","pid: "+pid+" pname: "+pname);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}。