Android实现下拉菜单Spinner效果

作者:lijiao 时间:2022-03-21 07:27:07 

Android 中下拉菜单,即如html中的<select>,关键在于调用setDropDownViewResource方法,以XML的方式定义下拉菜单要显示的模样

1.1.activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
 tools:context="com.rj141.sb.kongjian.MainActivity">

<LinearLayout
   android:orientation="horizontal"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
   <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textSize="18dp"
     android:text="请选择您最喜欢的水果:" />

<Spinner
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:id="@+id/spinner" />
 </LinearLayout>

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textSize="18dp"
   android:id="@+id/tv" />

</LinearLayout>

Spinner是下拉列表的组件

1.2.MainActivity.class


public class MainActivity extends AppCompatActivity {

private Spinner s;
 String[] data=new String[]{"苹果","雪梨","西瓜","葡萄","橙子","草莓"};
 private TextView tv;

@Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

tv= (TextView) this.findViewById(R.id.tv);
   s= (Spinner) this.findViewById(R.id.spinner);
   s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data));
   s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
     @Override
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
       String str=data[position];
       tv.setText("最喜欢的水果是:"+str);
     }
     @Override
     public void onNothingSelected(AdapterView<?> parent) {
     }
   });
 }
}
s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data));android.R.layout.simple_list_item_1是指安卓自带的下拉列表格式,data是数据源;
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()){..};是下拉列表的监听

效果图:

Android实现下拉菜单Spinner效果

标签:Android,下拉菜单,Spinner
0
投稿

猜你喜欢

  • 简单了解4种分布式session解决方案

    2023-08-09 11:45:49
  • Java使用wait和notify实现线程之间的通信

    2022-07-20 16:05:02
  • Hadoop组件简介

    2023-08-20 14:07:00
  • java 获取当前函数名的实现代码

    2021-09-21 03:32:45
  • Java Bean 作用域及它的几种类型介绍

    2022-12-02 20:39:42
  • 简介Java的Hibernate框架中的Session和持久化类

    2023-04-17 14:41:13
  • 浅谈@PostConstruct不被调用的原因

    2023-03-22 11:40:29
  • Spring中XML schema扩展机制的深入讲解

    2022-06-29 07:44:15
  • java ReentrantLock条件锁实现原理示例详解

    2023-12-12 02:36:13
  • 关于springboot配置文件密文解密方式

    2023-11-09 04:21:24
  • C#中const,readonly和static关键字的用法介绍

    2021-08-17 15:33:45
  • SpringBoot配置GlobalExceptionHandler全局异常处理器案例

    2023-06-11 12:14:36
  • C语言字符串操作总结大全(超详细)

    2023-07-06 15:14:56
  • C#使用LINQ查询表达式的基本子句总结

    2022-08-05 05:09:41
  • java实现随机验证码图片生成

    2022-12-08 02:27:00
  • Android Flutter实现仿闲鱼动画效果

    2023-07-15 15:32:47
  • Java毕业设计实战之二手书商城系统的实现

    2022-03-21 14:06:20
  • android TextView实现跑马灯效果

    2023-07-27 16:35:31
  • Mybatis分页插件PageHelper的使用详解

    2022-02-26 16:43:05
  • 基于Java解决华为机试之字符串加解密 

    2022-10-12 17:32:39
  • asp之家 软件编程 m.aspxhome.com