Spinner使用方法

2024-09-04 22:18
文章标签 使用 方法 spinner

本文主要是介绍Spinner使用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android中的Spinner和VC中的列表框很类似。这里做个小例子,是为笔记。

示例效果

ui

下拉的效果:

UI2

布局文件

Activity的布局my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceLarge"android:text="@string/spinner_hint" /><Spinnerandroid:id="@+id/spinner"android:layout_width="match_parent"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceLarge"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/result_hint"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceLarge"android:text="@string/result_hint" /><!-- android:hint="@string/result" --><EditTextandroid:id="@+id/result"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="none"android:textAppearance="?android:attr/textAppearanceLarge"/></LinearLayout></LinearLayout>

Spinner显示的布局spinner_layout.xml

这里直接简化了,显示一个简单的TextView。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/calculate_result"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceLarge"/></LinearLayout>

注:可以增加padding属性,优化显示效果。如:

<!-- 
android:layout_marginBottom="6dip" 
android:layout_marginLeft="10dip" 
android:layout_marginTop="6dip" 
android:layout_marginRight="2dip" 
--><!-- 
android:paddingBottom="3dip"
android:paddingEnd="4dip"
android:paddingStart="4dip"
android:paddingTop="3dip" 
-->

代码

省略一些自动生成的代码。

public static final String TAG = "MainActivity";private int[] values = {1, 3, 5, 7, 9};private Spinner spinner = null;
private EditText result = null;
private TextView result_hint = null;@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_layout);spinner = (Spinner) this.findViewById(R.id.spinner);spinner.setAdapter(new MySpinnerAdapter());spinner.setOnItemSelectedListener(new OnItemSelectedListener() {public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {int value = values[position];int cal_result = value * value;result.setText("" + cal_result);result_hint.setText(value + " * " + value + "=");}public void onNothingSelected(AdapterView<?> parent) {}});result = (EditText) this.findViewById(R.id.result);result.setInputType(InputType.TYPE_NULL);  result.setTextColor(Color.GRAY); result_hint = (TextView) this.findViewById(R.id.result_hint);
}private class MySpinnerAdapter extends BaseAdapter {public int getCount() {return values.length;}public Object getItem(int position) {return values[position];}public long getItemId(int position) {return position;}public View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) {convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.spinner_layout,null);}TextView textView = (TextView) convertView.findViewById(R.id.calculate_result);textView.setText("" + values[position]);return convertView;}}

要点

  • 在布局文件中增加Spinner
  • 为Spinner定义一个布局文件
  • 为Spinner定义一个Adapter,实现数据和视图的映射(MVC)
  • 为Spinner定义事件处理,通常即为OnItemSelectedListener。

ArrayAdapter

使用场景

在上面的例子中,使用的数据源是一个数组,而且只是让每个数字显示在Spinner的layout(spinner_layout.xml)的一个TextView中。

对于这种情况,直接使用ArrayAdapter以及android.R.layout.simple_spinner_item就够了。但需要修改values的类型为Integer[]:

private Integer[] values = {1, 3, 5, 7, 9};

适配器部分改为:

spinner = (Spinner) this.findViewById(R.id.spinner);ArrayAdapter<Integer> arrayAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item,values);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);//spinner.setAdapter(new MySpinnerAdapter());

simple_spinner_item

android.R.layout.simple_spinner_item是Android平台提供的,定义如下:

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1"style="?android:attr/spinnerItemStyle"android:singleLine="true"android:layout_width="match_parent"android:layout_height="wrap_content"android:ellipsize="marquee"android:textAlignment="inherit"/>

simple_spinner_dropdown_item

R.layout.simple_spinner_dropdown_item也是Android平台定义的,如下:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1"style="?android:attr/spinnerDropDownItemStyle"android:singleLine="true"android:layout_width="match_parent"android:layout_height="?android:attr/dropdownListPreferredItemHeight"android:ellipsize="marquee"android:textAlignment="inherit"/>

这篇关于Spinner使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1137184

相关文章

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

MySQL 打开binlog日志的方法及注意事项

《MySQL打开binlog日志的方法及注意事项》本文给大家介绍MySQL打开binlog日志的方法及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、默认状态二、如何检查 binlog 状态三、如何开启 binlog3.1 临时开启(重启后失效)

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项