欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android下拉列表(Spinner)效果(使用C#和Java分别实现)

程序员文章站 2022-06-10 19:09:56
效果如下: c#实现代码 using android.app; using android.os; using android.widget; n...

效果如下:

Android下拉列表(Spinner)效果(使用C#和Java分别实现)

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>