安卓控件使用系列25:Spinner下拉列表控件的使用方法
来源:互联网 发布:搜索引擎优化实战培训 编辑:程序博客网 时间:2024/05/16 03:39
安卓中下拉列表控件是经常使用的控件之一,下面我们一起分享一下它的使用方法。
这个例子实现的是两个下拉控件的效果,第一个下拉控件实现的是通过下拉选择不同文字;第二个下拉控件实现的是通过下拉选择不同的图文混排。
整体思路:在xml文件中添加两个Spinner控件;定义一个MyAdapter类,在getData方法中返回一个动态数组,数组中添加了几个文字信息,在getListMaps方法中返回Map类型动态数组,数组中添加了几个Map类型的图文混排,这个类作为数据的适配器;在活动中定义一个ArrayAdapter类型的适配器获取getData方法中数据,并绑定到第一个Spinner控件上,定义一个SimpleAdapter类型的适配器获取getListMaps方法的数据,并绑定到第二个Spinner控件上;对第二个Spinner控件添加setOnItemSelectedListener这个事件,在事件中获取当前选择的文字信息,并作为该应用程序的标题。
activity_main.xml文件:
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Spinner android:id="@+id/spinner2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>MainActivity.java文件:
private Spinner spinner; private Spinner spinner2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);spinner=(Spinner)findViewById(R.id.spinner);List<String> list=MyAdapter.getData();ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,list);spinner.setAdapter(adapter);spinner2=(Spinner)findViewById(R.id.spinner2);//List<Map<String,Object>>List<Map<String, Object>> listmaps=MyAdapter.getListMaps();SimpleAdapter simpleAdapter=new SimpleAdapter(MainActivity.this,listmaps,R.layout.item,new String[]{"ivLogo","applicationName"},new int[]{R.id.imageview,R.id.textview});spinner2.setAdapter(simpleAdapter);spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view,int position, long id) {// TODO Auto-generated method stubString appName=((Map<String, Object>)spinner2.getItemAtPosition(position)).get("applicationName").toString();setTitle(appName);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});}
MyAdapter.java文件:
public class MyAdapter {public MyAdapter() {// TODO Auto-generated constructor stub}public static List<String> getData(){List<String> list=new ArrayList<String>();list.add("北京");list.add("上海");list.add("广州");return list;} public static List<Map<String, Object>> getListMaps(){ List<Map<String, Object>> list=new ArrayList<Map<String,Object>>(); Map<String, Object> map1=new HashMap<String, Object>(); map1.put("ivLogo", R.drawable.btn_dialog); map1.put("applicationName", "罗志祥"); Map<String, Object> map2=new HashMap<String, Object>(); map2.put("ivLogo", R.drawable.btn_default); map2.put("applicationName", "庾澄庆"); list.add(map1); list.add(map2); return list; }}
0 0
- 安卓控件使用系列25:Spinner下拉列表控件的使用方法
- 安卓开发16:Spinner 下拉列表控件
- 安卓控件使用系列34:ListView列表控件显示表单的使用方法1
- 安卓控件使用系列35:ListView列表控件显示表单的使用方法2
- Spinner下拉列表控件
- Spinner控件:下拉列表
- Spinner类控件的使用(下拉列表的使用)
- Spinner下拉列表控件属性的使用细节总结
- Android 下拉列表Spinner控件
- android 控件 下拉列表(Spinner)
- Spinner控件显示下拉列表
- android----Spinner下拉列表控件
- Material-Spinner下拉列表控件
- 下拉列表控件Spinner(1)
- 安卓控件使用系列14:ImageView的基本使用方法
- 【Android 开发】:UI控件之 Spinner 下拉列表控件的使用
- Android下拉框控件Spinner的使用
- Android下拉框控件Spinner的使用
- Reverse Linked List II
- 会话管理技术Cookie&Session
- uva6195
- CRC-CCITT算法
- Proxy
- 安卓控件使用系列25:Spinner下拉列表控件的使用方法
- 双链表的实现C++封装
- Ubuntu用Wine运行Windows应用程序和卸载
- hdu 3971
- 中兴f402 412等破解超级密码、破解用户限制、关闭远程控制、恢复路由器拨号
- 学习与实践Cadence总结
- Ubuntu 15.04 clang++ 3.6 编译boost 1.59/1.55
- 山峰
- 将矩阵顺时针旋转90度 leetCode:Rotate Image