Android实现下拉菜单Spinner效果
作者:lijiao 发布时间:2022-03-21 07:27:07
标签:Android,下拉菜单,Spinner
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()){..};是下拉列表的监听
效果图:
0
投稿
猜你喜欢
- 概述工作电脑用了3年多了,100G的C盘也快吃不消了,每次打开看到C盘红了,总要用清理工具清理一下子.不知道怎么最近清理工具清理
- 一、微服务结构微服务这种方案需要技术框架来落地,全球的互联网公司都在积极尝试自己的微服务落地技术。在国内最知名的就是SpringCloud和
- 本文实例为大家分享了Unity Shader实现描边OutLine效果的具体代码,供大家参考,具体内容如下Shader实现描边流程大致为:对
- 有个小伙伴遇到了这样一个问题,就是AutoCompleteTextView实现自动填充的功能。同时要具备手机格式化的功能。下拉列表最后一行是
- 背景使用flowable自带的flowable-ui制作流程图使用springboot开发流程使用的接口完成流程的业务功能一、flowabl
- 本文作者:Spring_ZYL文章来源:https://blog.csdn.net/gozhuyinglong版权声明:本文版权归作者所有,
- 1.新建一个项目2.给项目添加引用:Microsoft Excel 12.0 Object Library (2007版本)using Ex
- 本文实例讲述了C#自定义签名章实现方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Coll
- 如果没有安装过maven,是用的idea自带的maven,那就是idea的安装目录下 /plugins/maven/lib/maven3这个
- 问题描述:在windows系统下,idea中,操作terminal控制台,使用git log查看日志时,出现如下乱码为什么参考网上很多的gi
- java读取word文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的API估计
- 今日遇到一个问题:springboot需要获取到一个自定义名称文件夹下的静态资源(图片等),并且文件夹的路径不在classPath下面,而是
- 图片上传功能是我们web里面经常用到的,获得的方式也有很多种,这里我用的是request.getInputStream()获取文件流的方式。
- 实现步骤step1:添加乐观锁 * MP的其他 * 功能可以参考官网@Beanpublic MybatisPlusInterceptor m
- 本文实例为大家分享了Java实现马踏棋盘的具体代码,供大家参考,具体内容如下马在某个点最多可能有8种走法,用递归和回溯实现。注:代码中,查找
- 1.首先要导入json相关的jar包引入的jar包:(版本自行定义,可以选用使用人数偏多的版本,这样比较稳定)commons-beanuti
- import java.io.UnsupportedEncodingException;import java.security.Inval
- AssertJ是我目前见过的最强大的断言api,没有之一。官网传送门为什么使用assertJ?1、流式断言,代码即用例,直观易懂。举个例子:
- java的构造函数是一个非常重要的作用,首先java里的构造函数是可以重载的,而且因为也是可以继承在父类的构造函数,所以在子类里,首先必然是
- HashMap 概述HashMap 是通过 put(key,value) 存储,get(key)来获取。当传入 key 时,HashMap