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

Android编程下拉菜单spinner用法小结(附2则示例)

程序员文章站 2023-12-19 10:18:46
本文较为详细的总结分析了android编程下拉菜单spinner用法。分享给大家供大家参考,具体如下: spinner控件也是一种列表类型的控件,它的继承关系如下: j...

本文较为详细的总结分析了android编程下拉菜单spinner用法。分享给大家供大家参考,具体如下:

spinner控件也是一种列表类型的控件,它的继承关系如下:

java.lang.object
   ↳ android.view.view
     ↳ android.view.viewgroup
       ↳ android.widget.adapterview<textends android.widget.adapter>
         ↳ android.widget.absspinner
          ↳ android.widget.spinner

android.widget.spinner继承了android.view.viewgroup类

在android的ui开发中,spinner(下拉列表)总是可以用到的,一个简单的自定义spinner制作我们只需要记住这重要的五步,一个spinner就可以应用而生了。

(1)新建一个android工程,名称为spinner。同时建一个activity,名称为spinneractivity。

Android编程下拉菜单spinner用法小结(附2则示例)

(2)修改res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 > 
 <textview 
 android:id = "@+id/textview_city" 
 android:layout_width = "wrap_content" 
 android:layout_height = "wrap_content" 
 android:text = "所在城市:" 
 /> 
 <!-- 定义一个城市信息下拉菜单--> 
 <spinner 
 android:id = "@+id/spinner_city" 
 android:layout_width = "wrap_content" 
 android:layout_height ="wrap_content" > 
 </spinner > 
</linearlayout>

(3)spinneractivity.java 代码

package cn.com; 
import java.util.arraylist; 
import java.util.list; 
import android.app.activity; 
import android.os.bundle; 
import android.view.motionevent; 
import android.view.view; 
import android.widget.adapterview; 
import android.widget.arrayadapter; 
import android.widget.spinner; 
import android.widget.textview; 
public class spinneractivity extends activity { 
 /** called when the activity is first created. */ 
 private list<string> list = new arraylist<string>(); 
 private textview mytextview; 
 private spinner myspinner; 
 private arrayadapter<string> adapter; 
 @override 
 public void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.main); 
 //第一步:添加一个下拉列表项的list,这里添加的项就是下拉列表的菜单项
 list.add("北京"); 
 list.add("上海"); 
 list.add("深圳"); 
 list.add("福州"); 
 list.add("厦门"); 
 mytextview = (textview)findviewbyid(r.id.textview_city); 
 myspinner = (spinner)findviewbyid(r.id.spinner_city); 
 //第二步:为下拉列表定义一个适配器,这里就用到里前面定义的list。 
 adapter = new arrayadapter<string>(this,android.r.layout.simple_spinner_item, list);
 //第三步:为适配器设置下拉列表下拉时的菜单样式。 
 adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); 
 //第四步:将适配器添加到下拉列表上 
 myspinner.setadapter(adapter); 
 //第五步:为下拉列表设置各种事件的响应,这个事响应菜单被选中 
 myspinner.setonitemselectedlistener(new spinner.onitemselectedlistener(){ 
  public void onitemselected(adapterview<?> arg0, view arg1, int arg2, long arg3) { 
  // todo auto-generated method stub 
  /* 将所选myspinner 的值带入mytextview 中*/ 
  mytextview.settext("您选择的是:"+ adapter.getitem(arg2)); 
  /* 将myspinner 显示*/ 
  arg0.setvisibility(view.visible); 
  } 
  public void onnothingselected(adapterview<?> arg0) { 
  // todo auto-generated method stub 
  mytextview.settext("none"); 
  arg0.setvisibility(view.visible); 
  } 
 }); 
 /*下拉菜单弹出的内容选项触屏事件处理*/ 
 myspinner.setontouchlistener(new spinner.ontouchlistener(){ 
  public boolean ontouch(view v, motionevent event) { 
  // todo auto-generated method stub 
  /** 
   * 
   */ 
  return false; 
  } 
 }); 
 /*下拉菜单弹出的内容选项焦点改变事件处理*/ 
 myspinner.setonfocuschangelistener(new spinner.onfocuschangelistener(){ 
 public void onfocuschange(view v, boolean hasfocus) { 
  // todo auto-generated method stub 
 } 
 }); 
 } 
}

运行结果如下图所示:

Android编程下拉菜单spinner用法小结(附2则示例)

Android编程下拉菜单spinner用法小结(附2则示例)

补充:spinner控件用法示例2则

用法1:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/container"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 tools:context="com.example.mars_2100_spinner.mainactivity"
 tools:ignore="mergerootframe" >
<spinner android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:id="@+id/spinnerid"/>
</linearlayout>

package com.example.mars_2100_spinner;
import android.support.v7.app.actionbaractivity;
import android.support.v7.app.actionbar;
import android.support.v4.app.fragment;
import android.os.bundle;
import android.view.layoutinflater;
import android.view.menu;
import android.view.menuitem;
import android.view.view;
import android.view.viewgroup;
import android.widget.adapterview;
import android.widget.arrayadapter;
import android.widget.spinner;
import android.widget.toast;
import android.os.build;
public class mainactivity extends actionbaractivity {
 private static final string[] m_arr = {"第一组","第二组","第三组"};
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 spinner _spin = (spinner)findviewbyid(r.id.spinnerid);
 arrayadapter<string> ada = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, m_arr);
 ada.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);
 _spin.setadapter(ada);
 _spin.setonitemselectedlistener(new spinner.onitemselectedlistener(){
  public void onitemselected(adapterview<?> arg0, view arg1, int arg2, long arg3){
  disptoast("选择的是"+m_arr[arg2]);
  arg0.setvisibility(view.visible);
  }
  public void onnothingselected(adapterview<?> arg0){
  //
  }
 });
 /*if (savedinstancestate == null) {
  getsupportfragmentmanager().begintransaction().add(r.id.container, new placeholderfragment()).commit();
 }*/
 }
 public void disptoast(string str){
 toast.maketext(this, str, toast.length_short).show();
 }
 @override
 public boolean oncreateoptionsmenu(menu menu) {
 // inflate the menu; this adds items to the action bar if it is present.
 getmenuinflater().inflate(r.menu.main, menu);
 return true;
 }
 @override
 public boolean onoptionsitemselected(menuitem item) {
 // handle action bar item clicks here. the action bar will
 // automatically handle clicks on the home/up button, so long
 // as you specify a parent activity in androidmanifest.xml.
 int id = item.getitemid();
 if (id == r.id.action_settings) {
  return true;
 }
 return super.onoptionsitemselected(item);
 }
 /**
 * a placeholder fragment containing a simple view.
 */
 public static class placeholderfragment extends fragment {
 public placeholderfragment() {
 }
 @override
 public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {
  view rootview = inflater.inflate(r.layout.fragment_main, container, false);
  return rootview;
 }
 }
}

用法2:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/container"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 tools:context="com.example.mars_2100_spinner.mainactivity"
 tools:ignore="mergerootframe" >
<spinner android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:id="@+id/spinnerid2"/>
<textview android:id="@+id/textview1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"/>
<spinner android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:id="@+id/spinnerid3"/>
<textview android:id="@+id/textview2"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"/>
</linearlayout>

package com.example.mars_2100_spinner;
import android.support.v7.app.actionbaractivity;
import android.support.v7.app.actionbar;
import android.support.v4.app.fragment;
import android.app.activity;
import android.os.bundle;
import android.view.layoutinflater;
import android.view.menu;
import android.view.menuitem;
import android.view.view;
import android.view.viewgroup;
import android.widget.adapterview;
import android.widget.adapterview.onitemselectedlistener;
import android.widget.arrayadapter;
import android.widget.spinner;
import android.widget.textview;
import android.os.build;
public class main02activity extends activity {
 static final string[] constellations = new string[] { "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天枰座", "天蝎座", "射手座", "魔蝎座", "水瓶座", "双鱼座" };
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main02);
 final textview txt2 = (textview) findviewbyid(r.id.textview1);
 spinner s1 = (spinner) findviewbyid(r.id.spinnerid2);
 final arrayadapter<charsequence> adapter = arrayadapter.createfromresource(this, r.array.planets_array, android.r.layout.simple_spinner_item);
 adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);
 s1.setadapter(adapter);
 s1.setprompt("测试");
 s1.setonitemselectedlistener(new onitemselectedlistener() {
  @override
  public void onitemselected(adapterview<?> parent, view view, int position, long id) {
  txt2.settext(adapter.getitem(position).tostring());
  }
  @override
  public void onnothingselected(adapterview<?> parent) {
  txt2.settext("没选中的");
  }
 });
 final textview txt4 = (textview) findviewbyid(r.id.textview2);
 spinner s2 = (spinner) findviewbyid(r.id.spinnerid3);
 arrayadapter<charsequence> adapterc = new arrayadapter<charsequence>(this, android.r.layout.simple_spinner_item, constellations);
 adapterc.setdropdownviewresource(android.r.layout.simple_spinner_item);
 s2.setadapter(adapterc);
 s2.setonitemselectedlistener(new onitemselectedlistener() {
  public void onitemselected(adapterview<?> parent, view view, int position, long id) {
  txt4.settext(constellations[position]);
  }
  public void onnothingselected(adapterview<?> parent) {
  txt4.settext("没选中的");
  }
 });
 }
 @override
 public boolean oncreateoptionsmenu(menu menu) {
 // inflate the menu; this adds items to the action bar if it is present.
 getmenuinflater().inflate(r.menu.main02, menu);
 return true;
 }
 @override
 public boolean onoptionsitemselected(menuitem item) {
 // handle action bar item clicks here. the action bar will
 // automatically handle clicks on the home/up button, so long
 // as you specify a parent activity in androidmanifest.xml.
 int id = item.getitemid();
 if (id == r.id.action_settings) {
  return true;
 }
 return super.onoptionsitemselected(item);
 }
 /**
 * a placeholder fragment containing a simple view.
 */
 public static class placeholderfragment extends fragment {
 public placeholderfragment() {
 }
 @override
 public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {
  view rootview = inflater.inflate(r.layout.fragment_main02, container, false);
  return rootview;
 }
 }
}

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="app_name">mars_2100_spinner</string>
 <string name="hello_world">hello world!</string>
 <string name="action_settings">settings</string>
 <string-array name="planets_array">
 <item>mercury</item>
 <item>venus</item>
 <item>earth</item>
 <item>mars</item>
 <item>jupiter</item>
 <item>saturn</item>
 <item>uranus</item>
 <item>neptune</item>
 </string-array>
 <string name="title_activity_main02">main02activity</string>
</resources>

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: