Android编程之下拉菜单Spinner控件用法示例
程序员文章站
2023-11-21 17:53:16
本文实例讲述了android下拉菜单spinner控件用法。分享给大家供大家参考,具体如下:
activity_main.xml
本文实例讲述了android下拉菜单spinner控件用法。分享给大家供大家参考,具体如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:textsize="25sp" android:textcolor="#ff0000" /> <spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> </linearlayout>
mainactivity.java
package com.example.hello; import java.util.arraylist; import java.util.list; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemselectedlistener; import android.widget.arrayadapter; import android.widget.spinner; import android.widget.textview; public class mainactivity extends actionbaractivity implements onitemselectedlistener{ private textview textview; private spinner spinner; private list<string> list; private arrayadapter<string> adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview = (textview) findviewbyid(r.id.textview); textview.settext("您选择的城市是:"); spinner = (spinner) findviewbyid(r.id.spinner); list = new arraylist<string>(); list.add("北京"); list.add("上海"); list.add("广州"); list.add("深圳"); adapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item,list); adapter.setdropdownviewresource(android.r.layout.simple_dropdown_item_1line); spinner.setadapter(adapter); spinner.setonitemselectedlistener(this); } @override public void onitemselected(adapterview<?> arg0, view arg1, int arg2,long arg3) { string cityname = adapter.getitem(arg2); //string city = list.get(arg2); textview.settext("您选择的城市是:"+cityname); } @override public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub } }
更多关于android相关内容感兴趣的读者可查看本站专题:《android视图view技巧总结》、《android布局layout技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。