Spinner 用法
Posted lzsin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spinner 用法相关的知识,希望对你有一定的参考价值。
布局文件
</Spinner>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spinner弹框模式"/>
<Spinner
android:id="@+id/SpinnerTK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dialog">
</Spinner>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spinner带标题的弹框模式"/>
<Spinner
android:id="@+id/SpinnerTKTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="@string/prompt"
android:spinnerMode="dialog">
</Spinner>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spinner复杂下拉框"/>
<Spinner
android:id="@+id/SpinnerImageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="@string/prompt"
android:spinnerMode="dropdown">
</Spinner>
后台代码
Spinner spxl = (Spinner) findViewById(R.id.SpinnerXL);
Spinner sptk = (Spinner) findViewById(R.id.SpinnerTK);
Spinner sptktitle = (Spinner) findViewById(R.id.SpinnerTKTitle);
List
for (int ii = 0; ii < 50; ii++) {
listdata.add("数据" + ii);
}
ArrayAdapter
spxl.setAdapter(adapter);
sptk.setAdapter(adapter);
sptktitle.setAdapter(adapter);
spxl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(BtnActivity.this, "选择了:" + parent.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Spinner spimagetext = (Spinner) findViewById(R.id.SpinnerImageText);
list = getData();
MyCustomAdapter myCustomAdapter = new MyCustomAdapter(list, BtnActivity.this);
spimagetext.setAdapter(myCustomAdapter);
spimagetext.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (!firstSelect) {
Toast.makeText(BtnActivity.this, "你选择了:" + list.get(position).get("name").toString(),
Toast.LENGTH_SHORT).show();
} else {
firstSelect = false;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Button btntab=(Button)findViewById(R.id.btntab);
btntab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(BtnActivity.this,TabBottom.class);
startActivity(intent);
}
});
}
ArrayList<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> map;
private boolean firstSelect=true;
private ArrayList<Map<String,Object>> getData() {
map=new HashMap<String, Object>();
map.put("image",R.drawable.ic_home_black_24dp);
map.put("name","日历");
map.put("desc","descdescdescdesc");
list.add(map);
map=new HashMap<String, Object>();
map.put("image",R.drawable.ic_dashboard_black_24dp);
map.put("name","相机");
map.put("desc","descdescdescdesc");
list.add(map);
map=new HashMap<String, Object>();
map.put("image",R.drawable.ic_notifications_black_24dp);
map.put("name","闹钟");
map.put("desc","descdescdescdesc");
list.add(map);
map=new HashMap<String, Object>();
map.put("image",R.drawable.ic_home_black_24dp);
map.put("name","游戏控制");
map.put("desc","descdescdescdesc");
list.add(map);
return list;
}
}
以上是关于Spinner 用法的主要内容,如果未能解决你的问题,请参考以下文章