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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
简单了解4种分布式session解决方案
2023-08-09 11:45:49
![](https://img.aspxhome.com/file/2023/7/57717_0s.png)
Java使用wait和notify实现线程之间的通信
2022-07-20 16:05:02
![](https://img.aspxhome.com/file/2023/8/78298_0s.jpg)
Hadoop组件简介
2023-08-20 14:07:00
![](https://img.aspxhome.com/file/2023/5/57725_0s.png)
java 获取当前函数名的实现代码
2021-09-21 03:32:45
Java Bean 作用域及它的几种类型介绍
2022-12-02 20:39:42
![](https://img.aspxhome.com/file/2023/9/75479_0s.png)
简介Java的Hibernate框架中的Session和持久化类
2023-04-17 14:41:13
浅谈@PostConstruct不被调用的原因
2023-03-22 11:40:29
![](https://img.aspxhome.com/file/2023/3/62503_0s.png)
Spring中XML schema扩展机制的深入讲解
2022-06-29 07:44:15
![](https://img.aspxhome.com/file/2023/4/77384_0s.png)
java ReentrantLock条件锁实现原理示例详解
2023-12-12 02:36:13
![](https://img.aspxhome.com/file/2023/8/66508_0s.png)
关于springboot配置文件密文解密方式
2023-11-09 04:21:24
![](https://img.aspxhome.com/file/2023/8/59418_0s.png)
C#中const,readonly和static关键字的用法介绍
2021-08-17 15:33:45
SpringBoot配置GlobalExceptionHandler全局异常处理器案例
2023-06-11 12:14:36
![](https://img.aspxhome.com/file/2023/6/60626_0s.png)
C语言字符串操作总结大全(超详细)
2023-07-06 15:14:56
C#使用LINQ查询表达式的基本子句总结
2022-08-05 05:09:41
![](https://img.aspxhome.com/file/2023/3/77253_0s.png)
java实现随机验证码图片生成
2022-12-08 02:27:00
![](https://img.aspxhome.com/file/2023/1/82301_0s.jpg)
Android Flutter实现仿闲鱼动画效果
2023-07-15 15:32:47
![](https://img.aspxhome.com/file/2023/3/87323_0s.jpg)
Java毕业设计实战之二手书商城系统的实现
2022-03-21 14:06:20
![](https://img.aspxhome.com/file/2023/4/72524_0s.jpg)
android TextView实现跑马灯效果
2023-07-27 16:35:31
Mybatis分页插件PageHelper的使用详解
2022-02-26 16:43:05
基于Java解决华为机试之字符串加解密
2022-10-12 17:32:39