Android开发之滑动数值选择器NumberPicker用法示例

作者:水中鱼之1999 时间:2022-08-04 07:22:36 

本文实例讲述了Android开发之滑动数值选择器NumberPicker用法。分享给大家供大家参考,具体如下:

简介:

NumberPicker: 用户既可以从键盘输值,也可以拖动来选择值

实际效果:

Android开发之滑动数值选择器NumberPicker用法示例

常用方法:

1. setMinValue() 设置组件支持的最小值

2. setMaxValue() 设置组建支持的最大值

3. setValue() 设置该组件的当前值

在布局文件中调用:


<?xml version="1.0" encoding="utf-8" ?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <TableRow
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1"
   android:orientation="vertical">
   <TextView
     android:text="选择时钟"
     android:textSize="20dp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>
   <NumberPicker
     android:id="@+id/np1"
     android:solidColor="@color/colorPrimaryDark"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:focusable="true"
     android:focusableInTouchMode="true"/>
 </TableRow>
 <TableRow
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1"
   android:orientation="vertical">
   <TextView
     android:text="选择分钟"
     android:textSize="20dp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>
   <NumberPicker
     android:id="@+id/np2"
     android:solidColor="@color/colorAccent"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:focusable="true"
     android:focusableInTouchMode="true" />
 </TableRow>
</TableLayout>

关于监听事件:

1. setOnValueChangedListener 调用监听事件

2. onValueChange 具体执行( int oldVal :之前详实的数值 , int newVal 改变或现时的数值)

具体实现方法:


public class MainActivity extends Activity {
 private NumberPicker np1,np2;
 //定义上下限具体值
 private int min = 10,max = 50;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   np1 = (NumberPicker) findViewById(R.id.np1);
   //设置np1的最大值只和最小值
   np1.setMinValue(0);
   np1.setMaxValue(23);
   //设置哪怕的当前值
   np1.setValue(min);
   np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
     @Override
     public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
       min = newVal;
       showSelectedPrice();
     }
   });
   np2 = (NumberPicker) findViewById(R.id.np2);
   //设置np1的最大值只和最小值
   np2.setMinValue(0);
   np2.setMaxValue(23);
   //设置哪怕的当前值
   np2.setValue(max);
   np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
     @Override
     public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
       min = newVal;
       showSelectedPrice();
     }
   });
 }
 private void showSelectedPrice(){
   Toast.makeText(MainActivity.this,"设定闹钟时间为:" + min + " : " + max,Toast.LENGTH_SHORT).show();
 }
}

希望本文所述对大家Android程序设计有所帮助。

来源:https://blog.csdn.net/qq_43377749/article/details/85002310

标签:Android,滑动数值选择器,NumberPicker
0
投稿

猜你喜欢

  • 基于Mock测试Spring MVC接口过程解析

    2023-11-27 12:04:30
  • JAVA并发图解

    2023-03-26 03:11:41
  • C# Winform中如何绘制动画示例详解

    2022-03-28 13:26:26
  • SpringBoot教程_创建第一个SpringBoot项目

    2022-02-19 23:12:54
  • Android实现3D层叠式卡片图片展示

    2022-04-22 16:39:19
  • 设置session有效时间的三种方式

    2022-03-11 15:55:15
  • 关于Springboot中JSCH的使用及说明

    2023-11-28 02:32:16
  • Java之SpringBoot自定义配置与整合Druid

    2022-09-28 06:23:06
  • Spring中基于xml的AOP的详细步骤

    2022-10-17 13:38:32
  • android全局监控click事件的四种方式(小结)

    2023-05-02 07:33:31
  • java编程实现根据EXCEL列名求其索引的方法

    2022-04-24 03:08:03
  • Java解压zip文件的关键代码

    2023-05-11 18:28:34
  • C#的FileInfo类实现文件操作实例

    2021-06-30 06:39:44
  • C# 创建EXCEL图表并保存为图片的实例

    2023-03-07 07:27:24
  • Java获取文件夹下所有文件名称的方法示例

    2023-08-08 08:01:26
  • SpringBoot整合支付宝APP支付

    2021-10-12 02:57:24
  • Android获取手机通讯录、sim卡联系人及调用拨号界面方法

    2021-07-02 00:41:07
  • mall整合SpringTask实现定时任务的方法示例

    2023-09-15 18:08:08
  • Java中的同步与异步详细介绍

    2023-06-23 00:42:08
  • Java实现求数组最长子序列算法示例

    2023-09-28 12:35:14
  • asp之家 软件编程 m.aspxhome.com