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

相关文章

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下