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将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

判断PyTorch是GPU版还是CPU版的方法小结

《判断PyTorch是GPU版还是CPU版的方法小结》PyTorch作为当前最流行的深度学习框架之一,支持在CPU和GPU(NVIDIACUDA)上运行,所以对于深度学习开发者来说,正确识别PyTor... 目录前言为什么需要区分GPU和CPU版本?性能差异硬件要求如何检查PyTorch版本?方法1:使用命

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Java中的工具类命名方法

《Java中的工具类命名方法》:本文主要介绍Java中的工具类究竟如何命名,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java中的工具类究竟如何命名?先来几个例子几种命名方式的比较到底如何命名 ?总结Java中的工具类究竟如何命名?先来几个例子JD

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依