解决方案

Android之Spinner使用详解

seo靠我 2023-09-25 02:30:48

Spinner(列表选择框)的基本使用

当我们的app需要用户输入数据时,除了让用户自己打字以外,还有一种比较贴心的设计: 列出一组选项让用户从中挑选,从而方便了我们的用户!

Spinner组件一共有两个SEO靠我,一个是本身的Spinner,一个是android.support.v7.widget.AppCompatSpinner

两者的区别在于v7内的Spinner是兼容低版本的,Spinner再高版本中才能SEO靠我使用的方法换了v7下的Spinner后可以一直兼容到2.1 (v7兼容到api7),初次之外两者的使用没有其他差别

一、常用的属性

属性名作用android:dropDownHorizontalOffseSEO靠我t设置列表框的水平偏移距离android:dropDownVerticalOffset设置列表框的水平竖直距离android:dropDownSelector列表框被选中时的背景android:droSEO靠我pDownWidth设置下拉列表框的宽度-android:gravity设置里面组件的对其方式android:popupBackground设置列表框的背景android:prompt设置对话框模式的SEO靠我列表框的提示信息(标题),只能够引用string.xml 中的资源id,而不能直接写字符串android:spinnerMode列表框的模式,有两个可选值: dialog:对话框风格的窗口 dropdSEO靠我own:下拉菜单风格的窗口(默认)可选属性:android:entries使用数组资源设置下拉列表框的列表项目

二、使用步骤

三、实例

项目结构:

在values文件夹下新建arrays.xml,来存放数组 SEO靠我 <?xml version="1.0" encoding="utf-8"?> <resources><string-array name="data"><item>倔强SEO靠我青铜</item><item>秩序白银</item><item>荣耀黄金</item><item>尊贵铂金</item><item>永恒钻石</item><item>至尊星耀</item><item>SEO靠我最强王者</item><item>荣耀王者</item></string-array> </resources> 编写布局文件 <?xml versiSEO靠我on="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/SEO靠我android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tSEO靠我ools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActiSEO靠我vity"android:orientation="vertical"android:gravity="center"><ImageViewandroid:layout_width="fill_parSEO靠我ent"android:layout_height="100dp"android:src="@drawable/bg"/><TextViewandroid:layout_marginTop="20dpSEO靠我"android:layout_width="200dp"android:layout_height="wrap_content"android:text="选择您的排位分段"android:textSEO靠我Color="#44BDED"android:textSize="18sp" /><Spinnerandroid:id="@+id/spinner_rank"android:layout_width=SEO靠我"220dp"android:layout_height="64dp"android:entries="@array/data"android:prompt="@string/title"androiSEO靠我d:spinnerMode="dialog" /><TextViewandroid:textStyle="bold"android:layout_width="wrap_content"androidSEO靠我:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="选择你的拿手英雄~"android:textColoSEO靠我r="#F5684A"android:textSize="18sp" /><Spinnerandroid:id="@+id/spinner_hero"android:layout_width="matSEO靠我ch_parent"android:layout_height="64dp" /><LinearLayoutandroid:layout_marginTop="30dp"android:layout_SEO靠我width="wrap_content"android:layout_height="40dp"android:gravity="center_vertical"><TextViewandroid:tSEO靠我extSize="20sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="你SEO靠我的段位是:"/><TextViewandroid:textSize="20sp"android:id="@+id/rank"android:layout_width="wrap_content"andSEO靠我roid:layout_height="wrap_content"android:text="倔强青铜"/></LinearLayout><LinearLayoutandroid:layout_widSEO靠我th="wrap_content"android:layout_height="50dp"android:gravity="center_vertical"><TextViewandroid:textSEO靠我Size="20sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="你的擅长SEO靠我的英雄是:"/><TextViewandroid:id="@+id/hero"android:textSize="20sp"android:layout_width="wrap_content"andSEO靠我roid:layout_height="wrap_content"android:text="亚瑟"/><ImageViewandroid:id="@+id/im_hero"android:layouSEO靠我t_width="48dp"android:layout_height="48dp"android:src="@drawable/yase"/></LinearLayout> </LiSEO靠我nearLayout> 编写实体类HeroBean,来进行动态的注入存放我们的选择英雄的名字和图片 package com.mq.spinner;public claSEO靠我ss HeroBean {private int icon;private String name;public HeroBean() {}public HeroBean(int icon, StriSEO靠我ng name) {this.icon = icon;this.name = name;}public int getIcon() {return icon;}public String getNamSEO靠我e() {return name;}public void setIcon(int icon) {this.icon = icon;}public void seName(String name) {SEO靠我this.name = name;} } 添加一个Spinner 的 item 的布局,命名为spinner_item.xml <?xml versioSEO靠我n="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/anSEO靠我droid"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="5dp"><SEO靠我ImageViewandroid:id="@+id/icon"android:layout_width="48dp"android:layout_height="48dp"android:src="@SEO靠我drawable/yase"/><TextViewandroid:id="@+id/name"android:layout_width="wrap_content"android:layout_heiSEO靠我ght="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="15dp"android:text="德玛西亚"SEO靠我android:textSize="16sp" /> </LinearLayout> 编写一个MyAdapter适配器,让Spinner返回选择的视图

Spinner SEO靠我继承自 AdapterView,也就是说它也通过 Adapter 来提供数据支持

Spinner 默认会选中第一个值,就是默认调用 spinner.setSection(0),我们也通过这个设置默认的选SEO靠我中值

package com.mq.spinner;import android.content.Context; import android.view.LayoutInflater;SEO靠我 import android.view.View; import android.view.ViewGroup; import android.widSEO靠我get.BaseAdapter; import android.widget.ImageView; import android.widget.TextView;impSEO靠我ort java.util.List;public class MyAdapter extends BaseAdapter {private List<HeroBean> mHeroBeans;priSEO靠我vate Context mContext;public MyAdapter(List<HeroBean> heroBeans, Context context) {this.mHeroBeans =SEO靠我 heroBeans;this.mContext = context;}@Overridepublic int getCount() {return mHeroBeans.size();}@OverrSEO靠我idepublic Object getItem(int i) {return mHeroBeans.get(i);}@Overridepublic long getItemId(int i) {reSEO靠我turn i;}@Overridepublic View getView(int position, View view, ViewGroup viewGroup) {LayoutInflater _SEO靠我LayoutInflater=LayoutInflater.from(mContext);view=_LayoutInflater.inflate(R.layout.spinner_item, nulSEO靠我l);if(view!=null){TextView textView=(TextView)view.findViewById(R.id.name);ImageView imageView=view.SEO靠我findViewById(R.id.icon);textView.setText(mHeroBeans.get(position).getName());imageView.setImageResouSEO靠我rce(mHeroBeans.get(position).getIcon());}return view;} } 片 编写活动MainActivitySEO靠我代码,代码有注释 public class MainActivity extends AppCompatActivity {private Spinner scope;private SEO靠我Spinner hero;private TextView t_rank,t_hero;private ImageView m_hero;private ArrayList<HeroBean> mDaSEO靠我ta = null;//存放英雄的名字和图片的实体类数组@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreSEO靠我ate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {SEO靠我scope=findViewById(R.id.spinner_rank);hero=findViewById(R.id.spinner_hero);t_hero=findViewById(R.id.SEO靠我hero);t_rank=findViewById(R.id.rank);m_hero=findViewById(R.id.im_hero);mData=new ArrayList<HeroBean>SEO靠我();mData.add(new HeroBean(R.drawable.sunwukong,"孙悟空"));mData.add(new HeroBean(R.drawable.zixia,"紫霞仙子SEO靠我"));mData.add(new HeroBean(R.drawable.diaocan,"貂蝉"));mData.add(new HeroBean(R.drawable.hanxin,"韩信"))SEO靠我;mData.add(new HeroBean(R.drawable.machao,"马超"));mData.add(new HeroBean(R.drawable.bailixuance,"百里玄策SEO靠我"));MyAdapter adapter = new MyAdapter(mData, this);//实例化适配器hero.setAdapter(adapter);//给hero设置适配器//给选SEO靠我择段位的spinner添加监听scope.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@OverridepuSEO靠我blic void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {t_rank.setText(scopeSEO靠我.getSelectedItem().toString());}@Overridepublic void onNothingSelected(AdapterView<?> adapterView) {SEO靠我}});//给选择英雄的spinner添加监听hero.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@OveSEO靠我rride //选中的时候执行的方法public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {SEO靠我Toast.makeText(MainActivity.this, mData.get(i).getName(), Toast.LENGTH_SHORT).show();t_hero.setText(SEO靠我mData.get(i).getName());//给TextView设置上选择的英雄名字m_hero.setImageResource(mData.get(i).getIcon());//给ImagSEO靠我eview上选择的英雄图片}@Overridepublic void onNothingSelected(AdapterView<?> adapterView) {}});} }

小结

SSEO靠我pinner会默认选中第一个值,就是默认调用spinner.setSection(0), 你可以通过这个设置默认的选中值会触发一次OnItemSelectedListener 事件,暂时没找到解决方法SEO靠我,下面折衷的处理是:添加一个boolean值,然后设置 为false,在onItemSelected时进行判断,false说明是默认触发的,不做任何操作 将boolean值设置为true;true的话SEO靠我则正常触发事件!加载数据源有两种方式: 通过xml加载,在实例中第一个选择段位就是使用xml来加载的,通过android:entries通过代码来加载数据源,在实例中的第二种选择英雄就是使用代码来加载SEO靠我数据源,可以用一个Arrays数组来存等。XML特点是方便快捷,缺点是不能动态改变要显示的数据,使用适配器特点是灵活多变,针对项目需求来选择.android:prompt 属性使用常见问题

设置之后不起SEO靠我作用:prompt属性只有在dialog状态才有用,所以要在xml中,将style设置为Widget.Spinner

prompt属性要用string下资源,不支持字符直接输入,否则会报错误SpinneSEO靠我r的菜单显示方式有两种,实例中两种都使用了 弹出框形式,通过属性android:spinnerMode="dialog"设置下拉框形式,通过属性android:spinnerMode="dropdowSEO靠我n"设置,默认是下拉框
“SEO靠我”的新闻页面文章、图片、音频、视频等稿件均为自媒体人、第三方机构发布或转载。如稿件涉及版权等问题,请与 我们联系删除或处理,客服邮箱:html5sh@163.com,稿件内容仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同 其观点或证实其内容的真实性。

网站备案号:浙ICP备17034767号-2