数值选择器(NumberPicker)的功能与用法

2023-12-22 10:58

本文主要是介绍数值选择器(NumberPicker)的功能与用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



     数值选择器用于让用户输入数值,用户既可以通过键盘输入数值,也可以通过拖动来选择数值。使用该组件常用如下三个方法。

  • setMinValue(int minVal):设置该组件支持的最小值。
  • setMaxValue(int maxVal):设置该组件支持的最大值。
  • setValue(int value):设置该组件的当前值。

     下面通过一个实例来介绍NumberPicker的功能与用法。

     实例:选择您意向的价格范围

     在该实例中,程序将使用两个NumberPicker来让用户选择价格,第一个NumberPicker用于选择低价,第二个NumberPicker用于选择高价。下面是该实例的布局文件。

     布局文件如下:    

    

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent">
<TableRow android:layout_width="fill_parent"android:layout_height="wrap_content" ><TextView android:text="选择低价:"android:layout_width="120dp"android:layout_height="wrap_content"/><NumberPicker android:id="@+id/np1"android:layout_width="match_parent"android:layout_height="80dp"android:focusable="true"android:focusableInTouchMode="true"/></TableRow><TableRow android:layout_width="fill_parent"android:layout_height="wrap_content"><TextView android:text="选择高价:"android:layout_width="120dp"android:layout_height="wrap_content"/><NumberPicker android:id="@+id/np2"android:layout_width="match_parent"android:layout_height="80dp"android:focusable="true"android:focusableInTouchMode="true"/></TableRow>
</TableLayout>

上面的布局文件中定义了两个NumberPicker,接下来Activity代码需要为这两个NumberPicker设置最小值、最大值,并为他们绑定事件监听器。下面是该Activity的后台代码。

package org.crazyit.helloworld;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.Toast;public class NumberPickerTest extends Activity {NumberPicker np1,np2;//定义最低价格、最高价格的初始值int minPrice=25,maxPrice=75;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.number_picker_test);np1=(NumberPicker)findViewById(R.id.np1);//设置np1的最小值和最大值 np1.setMinValue(10);np1.setMaxValue(50);//设置np1的当前值
        np1.setValue(minPrice);np1.setOnValueChangedListener(new OnValueChangeListener(){
@Overridepublic void onValueChange(NumberPicker picker, int oldVal,int newVal) {// TODO Auto-generated method stubminPrice=newVal;showSelectedPrice();}});np2=(NumberPicker)findViewById(R.id.np2);//设置np2的最小值和最大值  np2.setMinValue(60);np2.setMaxValue(100);//设置np2的当前值
        np2.setValue(maxPrice);
        np2.setOnValueChangedListener(new OnValueChangeListener(){@Overridepublic void onValueChange(NumberPicker picker, int oldVal,int newVal) {// TODO Auto-generated method stubmaxPrice=newVal;showSelectedPrice();}});}private void showSelectedPrice(){Toast.makeText(this, "您选择的价格为:"+minPrice+",最高价格为:"+maxPrice, Toast.LENGTH_SHORT).show();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.number_picker_test, menu);return true;}}

     上面两段粗体字代码的控制逻辑基本是相似的,它们都调用了NumberPicker的setMinValue()、setMaxValue()来设置该数值选择器的最小值、最大值和当前值。除此之外,程序还为两个日期选择器绑定了事件监听器:当它们的值发生改变时,将会激发相应的事件处理方法。
     运行该程序,并通过NumberPicker选择数值,将可以看到如下效果:

 

  

 

 

这篇关于数值选择器(NumberPicker)的功能与用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

python3 gunicorn配置文件的用法解读

《python3gunicorn配置文件的用法解读》:本文主要介绍python3gunicorn配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python3 gunicorn配置文件配置文件服务启动、重启、关闭启动重启关闭总结python3 gun

MySQL 中的 LIMIT 语句及基本用法

《MySQL中的LIMIT语句及基本用法》LIMIT语句用于限制查询返回的行数,常用于分页查询或取部分数据,提高查询效率,:本文主要介绍MySQL中的LIMIT语句,需要的朋友可以参考下... 目录mysql 中的 LIMIT 语句1. LIMIT 语法2. LIMIT 基本用法(1) 获取前 N 行数据(

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

C#中DrawCurve的用法小结

《C#中DrawCurve的用法小结》本文主要介绍了C#中DrawCurve的用法小结,通常用于绘制一条平滑的曲线通过一系列给定的点,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 如何使用 DrawCurve 方法(不带弯曲程度)2. 如何使用 DrawCurve 方法(带弯曲程度)3.使用Dr

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

pytorch之torch.flatten()和torch.nn.Flatten()的用法

《pytorch之torch.flatten()和torch.nn.Flatten()的用法》:本文主要介绍pytorch之torch.flatten()和torch.nn.Flatten()的用... 目录torch.flatten()和torch.nn.Flatten()的用法下面举例说明总结torch