Android下拉列表(Spinner)效果(使用C#和Java分别实现)
程序员文章站
2022-06-10 19:09:56
效果如下:
c#实现代码
using android.app;
using android.os;
using android.widget;
n...
效果如下:
c#实现代码
using android.app; using android.os; using android.widget; namespace spinnerdemo { [activity(label = "@string/applicationname", mainlauncher = true, icon = "@drawable/icon")] public class mainactivity : activity { private spinner _cityspinner; private textview _citynameview; private arrayadapter<string> _cityinfos; protected override void oncreate(bundle bundle) { base.oncreate(bundle); setcontentview(resource.layout.main); _cityspinner = findviewbyid<spinner>(resource.id.sp_city); _citynameview = findviewbyid<textview>(resource.id.txt_cityname); _cityinfos = new arrayadapter<string>(this, android.resource.layout.simplespinnerdropdownitem); _cityinfos.add("成都"); _cityinfos.add("兰州"); _cityinfos.add("武汉"); _cityinfos.add("上海"); _cityspinner.adapter = _cityinfos; _cityspinner.itemselected += cityselectedevent; } private void cityselectedevent(object sender, adapterview.itemselectedeventargs e) { _citynameview.text = _cityinfos.getitem(e.position); } } }
java实现代码
package com.example.halower.spinnerdemo; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.spinner; import android.widget.textview; public class mainactivity extends actionbaractivity { private spinner _cityspinner; private textview _citynameview; private arrayadapter<string> _cityinfo; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); _cityspinner =(spinner)findviewbyid(r.id.sp_city); _citynameview =(textview) findviewbyid(r.id.txt_cityname); _cityinfo =new arrayadapter<>(this,r.layout.support_simple_spinner_dropdown_item); _cityinfo.add("成都"); _cityinfo.add("兰州"); _cityinfo.add("武汉"); _cityinfo.add("上海"); _cityspinner.setadapter(_cityinfo); _cityspinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { _citynameview.settext(_cityinfo.getitem(position)); } @override public void onnothingselected(adapterview<?> parent) { } }); } }
layout
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/sp_city" android:layout_alignparenttop="true" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_toendof="@+id/txt_cityname" android:layout_torightof="@+id/txt_cityname" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="城市" android:id="@+id/txt_cityname" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> </relativelayout>
推荐阅读
-
Android自定义Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)
-
Android自定义Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)
-
Android下拉列表(Spinner)效果(使用C#和Java分别实现)
-
Android仿微信菜单(Menu)(使用C#和Java分别实现)
-
Android相册效果(使用C#和Java分别实现)
-
Android相册效果(使用C#和Java分别实现)
-
Android下拉列表(Spinner)效果(使用C#和Java分别实现)
-
Android仿微信菜单(Menu)(使用C#和Java分别实现)
-
分享一个Android仿微信菜单使用C#和Java分别实现的实例
-
分享一个Android仿微信菜单使用C#和Java分别实现的实例