Android之Spinner使用详解
Spinner(列表选择框)的基本使用
当我们的app需要用户输入数据时,除了让用户自己打字以外,还有一种比较贴心的设计: 列出一组选项让用户从中挑选,从而方便了我们的用户!
Spinner组件一共有两个,一个是本身的Spinner,一个是android.support.v7.widget.AppCompatSpinner
两者的区别在于v7内的Spinner是兼容低版本的,Spinner再高版本中才能使用的方法换了v7下的Spinner后可以一直兼容到2.1 (v7兼容到api7),初次之外两者的使用没有其他差别
一、常用的属性
属性名 | 作用 |
---|---|
android:dropDownHorizontalOffset | 设置列表框的水平偏移距离 |
android:dropDownVerticalOffset | 设置列表框的水平竖直距离 |
android:dropDownSelector | 列表框被选中时的背景 |
android:dropDownWidth | 设置下拉列表框的宽度- |
android:gravity | 设置里面组件的对其方式 |
android:popupBackground | 设置列表框的背景 |
android:prompt | 设置对话框模式的列表框的提示信息(标题),只能够引用string.xml 中的资源id,而不能直接写字符串 |
android:spinnerMode | 列表框的模式,有两个可选值: dialog:对话框风格的窗口 dropdown:下拉菜单风格的窗口(默认) |
可选属性:android:entries | 使用数组资源设置下拉列表框的列表项目 |
二、使用步骤
三、实例
项目结构:
- 在values文件夹下新建arrays.xml,来存放数组
<?xml version="1.0" encoding="utf-8"?>
<resources><string-array name="data"><item>倔强青铜</item><item>秩序白银</item><item>荣耀黄金</item><item>尊贵铂金</item><item>永恒钻石</item><item>至尊星耀</item><item>最强王者</item><item>荣耀王者</item></string-array>
</resources>
- 编写布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"android:orientation="vertical"android:gravity="center"><ImageViewandroid:layout_width="fill_parent"android:layout_height="100dp"android:src="@drawable/bg"/><TextViewandroid:layout_marginTop="20dp"android:layout_width="200dp"android:layout_height="wrap_content"android:text="选择您的排位分段"android:textColor="#44BDED"android:textSize="18sp" /><Spinnerandroid:id="@+id/spinner_rank"android:layout_width="220dp"android:layout_height="64dp"android:entries="@array/data"android:prompt="@string/title"android:spinnerMode="dialog" /><TextViewandroid:textStyle="bold"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="选择你的拿手英雄~"android:textColor="#F5684A"android:textSize="18sp" /><Spinnerandroid:id="@+id/spinner_hero"android:layout_width="match_parent"android:layout_height="64dp" /><LinearLayoutandroid:layout_marginTop="30dp"android:layout_width="wrap_content"android:layout_height="40dp"android:gravity="center_vertical"><TextViewandroid:textSize="20sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="你的段位是:"/><TextViewandroid:textSize="20sp"android:id="@+id/rank"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="倔强青铜"/></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="50dp"android:gravity="center_vertical"><TextViewandroid:textSize="20sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="你的擅长的英雄是:"/><TextViewandroid:id="@+id/hero"android:textSize="20sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="亚瑟"/><ImageViewandroid:id="@+id/im_hero"android:layout_width="48dp"android:layout_height="48dp"android:src="@drawable/yase"/></LinearLayout>
</LinearLayout>
- 编写实体类HeroBean,来进行动态的注入存放我们的选择英雄的名字和图片
package com.mq.spinner;public class HeroBean {private int icon;private String name;public HeroBean() {}public HeroBean(int icon, String name) {this.icon = icon;this.name = name;}public int getIcon() {return icon;}public String getName() {return name;}public void setIcon(int icon) {this.icon = icon;}public void seName(String name) {this.name = name;}
}
- 添加一个Spinner 的 item 的布局,命名为spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="5dp"><ImageViewandroid:id="@+id/icon"android:layout_width="48dp"android:layout_height="48dp"android:src="@drawable/yase"/><TextViewandroid:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="15dp"android:text="德玛西亚"android:textSize="16sp" />
</LinearLayout>
- 编写一个MyAdapter适配器,让Spinner返回选择的视图
Spinner 继承自 AdapterView,也就是说它也通过 Adapter 来提供数据支持
Spinner 默认会选中第一个值,就是默认调用 spinner.setSection(0),我们也通过这个设置默认的选中值
package com.mq.spinner;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;import java.util.List;public class MyAdapter extends BaseAdapter {private List<HeroBean> mHeroBeans;private Context mContext;public MyAdapter(List<HeroBean> heroBeans, Context context) {this.mHeroBeans = heroBeans;this.mContext = context;}@Overridepublic int getCount() {return mHeroBeans.size();}@Overridepublic Object getItem(int i) {return mHeroBeans.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic View getView(int position, View view, ViewGroup viewGroup) {LayoutInflater _LayoutInflater=LayoutInflater.from(mContext);view=_LayoutInflater.inflate(R.layout.spinner_item, null);if(view!=null){TextView textView=(TextView)view.findViewById(R.id.name);ImageView imageView=view.findViewById(R.id.icon);textView.setText(mHeroBeans.get(position).getName());imageView.setImageResource(mHeroBeans.get(position).getIcon());}return view;}
}
片
- 编写活动MainActivity代码,代码有注释
public class MainActivity extends AppCompatActivity {private Spinner scope;private Spinner hero;private TextView t_rank,t_hero;private ImageView m_hero;private ArrayList<HeroBean> mData = null;//存放英雄的名字和图片的实体类数组@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {scope=findViewById(R.id.spinner_rank);hero=findViewById(R.id.spinner_hero);t_hero=findViewById(R.id.hero);t_rank=findViewById(R.id.rank);m_hero=findViewById(R.id.im_hero);mData=new ArrayList<HeroBean>();mData.add(new HeroBean(R.drawable.sunwukong,"孙悟空"));mData.add(new HeroBean(R.drawable.zixia,"紫霞仙子"));mData.add(new HeroBean(R.drawable.diaocan,"貂蝉"));mData.add(new HeroBean(R.drawable.hanxin,"韩信"));mData.add(new HeroBean(R.drawable.machao,"马超"));mData.add(new HeroBean(R.drawable.bailixuance,"百里玄策"));MyAdapter adapter = new MyAdapter(mData, this);//实例化适配器hero.setAdapter(adapter);//给hero设置适配器//给选择段位的spinner添加监听scope.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {t_rank.setText(scope.getSelectedItem().toString());}@Overridepublic void onNothingSelected(AdapterView<?> adapterView) {}});//给选择英雄的spinner添加监听hero.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Override //选中的时候执行的方法public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {Toast.makeText(MainActivity.this, mData.get(i).getName(), Toast.LENGTH_SHORT).show();t_hero.setText(mData.get(i).getName());//给TextView设置上选择的英雄名字m_hero.setImageResource(mData.get(i).getIcon());//给Imageview上选择的英雄图片}@Overridepublic void onNothingSelected(AdapterView<?> adapterView) {}});}
}
小结
- Spinner会默认选中第一个值,就是默认调用spinner.setSection(0), 你可以通过这个设置默认的选中值
- 会触发一次OnItemSelectedListener 事件,暂时没找到解决方法,下面折衷的处理是:添加一个boolean值,然后设置 为false,在onItemSelected时进行判断,false说明是默认触发的,不做任何操作 将boolean值设置为true;true的话则正常触发事件!
- 加载数据源有两种方式:
- 通过xml加载,在实例中第一个选择段位就是使用xml来加载的,通过
android:entries
- 通过代码来加载数据源,在实例中的第二种选择英雄就是使用代码来加载数据源,可以用一个Arrays数组来存等。
- XML特点是方便快捷,缺点是不能动态改变要显示的数据,使用适配器特点是灵活多变,针对项目需求来选择.
- 通过xml加载,在实例中第一个选择段位就是使用xml来加载的,通过
- android:prompt 属性使用常见问题
设置之后不起作用:prompt属性只有在dialog状态才有用,所以要在xml中,将style设置为Widget.Spinner
prompt属性要用string下资源,不支持字符直接输入,否则会报错误 - Spinner的菜单显示方式有两种,实例中两种都使用了
- 弹出框形式,通过属性
android:spinnerMode="dialog"
设置 - 下拉框形式,通过属性
android:spinnerMode="dropdown"
设置,默认是下拉框
- 弹出框形式,通过属性
Android之Spinner使用详解相关推荐
- android jar 包 意见反馈功能,android重点jar包详解.docx
android重点jar包详解 深入理解View(一):从setContentView谈起 我们都知道?MVC,在Android中,这个?V?即指View,那我们今天就来探探View的究竟.在onCr ...
- Android应用坐标系统全面详解
Android应用坐标系统全面详解 原文链接:CSDN@工匠若水,http://blog.csdn.net/yanbober/article/details/50419117 1. 背景 去年有很多人 ...
- Android NFC开发实战详解
Android NFC开发实战详解 Android开发实战详解NFC国内第一本AndroidNFC开发书籍带你开启AndroidNFC开发的神秘之旅大综合案例帮助读者快速进入实战角色:WiFi快速连接 ...
- Android Gradle 自定义Task详解二:进阶
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78523958 本文出自[赵彦军的博客] 系列目录 Android Gradle使用 ...
- Android Gradle 自定义Task 详解
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/76408024 本文出自[赵彦军的博客] 系列目录 Android Gradle使用 ...
- android ------- 开发者的 RxJava 详解
在正文开始之前的最后,放上 GitHub 链接和引入依赖的 gradle 代码: Github: https://github.com/ReactiveX/RxJava https://githu ...
- Android自定义属性,format详解
1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name="名称"><attr name="bac ...
- android layout_width 属性,android:layout_weight属性详解
在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...
- 宏锦软件 Android 的 ListView 使用详解
宏锦软件爱好者在开发Android软件时,对ListView的使用有点陌生,于是翻了许多资料,这里给大家一份比较好的教程,希望有用. 在android开发中ListView是比较常用的组件,它以 ...
最新文章
- 《数据结构》知识点Day_02
- OpenGL textures纹理的实例
- 芯片巨头英特尔放弃竞争迷你计算机市场
- 【ArcGIS风暴】ArcGIS栅格数据(分区)统计方法总结
- Docker 资源限制之内存
- JDK1.7 深入理解 LinkedHashMap
- (原创)如何进行有符号小数乘法运算?(Verilog)
- 三次握手四次挥手必掌握,看了绝对不吃亏!
- HDOJ 1505 City Game
- 计算机控制技术课程设计温度控制系统,计算机控制技术课程设计PWM温度自动控制系统的设计...
- OpenGL编程指南(第七版)
- SPSS学习笔记(二)T检验
- 下周递交上市申请,腾讯持股的微医投资价值几何?
- Windows 11 将“扼杀”第三方浏览器?Firefox 绝地反击!
- 列名 计算机网络 无效,80004005 及其它错误消息的疑难解答
- 主流网站统计系统的评测
- h3c路由器msr2600-10,msr3600-28调试记录
- phpmail通过qq发邮箱失败_请问phpmailer发送Gmail总是失败是什么原因,qq邮件可以发送出去...
- Nginx-正向代理HTTPS
- Android模拟器下重力感应应用的开发-Simulator的使用
热门文章
- 小学教育如何利用计算机思维,浅谈如何在中小学编程教学中培养学生的计算思维...
- (筆記) 如何在字串中從指定字元抓到指定字元(pointer版)? (C/C++) (C)
- FFmpeg命令(七)、 音频与视频合并成视频
- ubuntu 16.04 + GTX1050安装nvidia驱动
- IOT语义互操作性之本体论 1
- C语言程序设计笔记(浙大翁恺版) 第二周:计算
- 如何成为荣耀开发者:注册与认证常见问题
- PDE——delta函数
- c语言编程顺序查找法,建立顺序表,实现顺序表的遍历,在顺序表中查找关键字为e的元素(c语言编写)...
- 每天数千个漏洞被公开 选什么工具能让漏洞追不上我?RASP介绍