当前位置: 首页 > 编程笔记 >

Android数字选择器NumberPicker使用详解

姬康平
2023-03-14
本文向大家介绍Android数字选择器NumberPicker使用详解,包括了Android数字选择器NumberPicker使用详解的使用技巧和注意事项,需要的朋友参考一下

数字选择器NumberPicker是Android3.0之后引入的一个控件,比较常用,比如说手机常用的闹钟,可以选择小时和分钟,如果你需要兼容3.0之前版本,GitHub上有开源的项目,具体的下载地址。本人就没有使用开源的项目,就简单的使用了NumberPicker显示一下效果,开始正题吧:

基础维护

开发东西先看下效果吧:

NumberPicker和TextView显示一下时间,线性布局,看下布局文件吧:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  tools:context="com.example.googlenumberpicker.MainActivity" >
 
  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="50dp"
    android:layout_gravity="center_horizontal" >
 
    <NumberPicker
      android:id="@+id/hourpicker"
      android:layout_width="40dp"
      android:layout_height="wrap_content" />
 
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:text="时" />
 
    <NumberPicker
      android:id="@+id/minuteicker"
      android:layout_width="40dp"
      android:layout_height="wrap_content" />
 
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:text="分" />
  </LinearLayout>
 
</LinearLayout>

Demo实现

字选择是可以滑动,所以需要定义一个OnValueChangeListener事件,OnScrollListener滑动事件,Formatter事件:

Formatter事件:

public String format(int value) {
    String tmpStr = String.valueOf(value);
    if (value < 10) {
      tmpStr = "0" + tmpStr;
    }
    return tmpStr;
  }

OnValueChangeListener事件:

public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
   Toast.makeText(
       this,
       "原来的值 " + oldVal + "--新值: "
           + newVal, Toast.LENGTH_SHORT).show();
 }

OnScrollListener滑动事件,滑动事件有三个状态:

SCROLL_STATE_FLING:手离开之后还在滑动

SCROLL_STATE_IDLE:不滑动

SCROLL_STATE_TOUCH_SCROLL:滑动中

public void onScrollStateChange(NumberPicker view, int scrollState) {
   switch (scrollState) {
   case OnScrollListener.SCROLL_STATE_FLING:
     Toast.makeText(this, "后续滑动(飞呀飞,根本停下来)", Toast.LENGTH_LONG)
         .show();
     break;
   case OnScrollListener.SCROLL_STATE_IDLE:
     Toast.makeText(this, "不滑动", Toast.LENGTH_LONG).show();
     break;
   case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
     Toast.makeText(this, "滑动中", Toast.LENGTH_LONG)
         .show();
     break;
   }
 }

初始化:

hourPicker=(NumberPicker) findViewById(R.id.hourpicker);
  minutePicker=(NumberPicker) findViewById(R.id.minuteicker);
  init();

init方法中,设置数字的最大值,最小值,以及滑动事件:

private void init() {
   hourPicker.setFormatter(this);
   hourPicker.setOnValueChangedListener(this);
   hourPicker.setOnScrollListener(this);
   hourPicker.setMaxValue(24);
   hourPicker.setMinValue(0);
   hourPicker.setValue(9);
   
   minutePicker.setFormatter(this);
   minutePicker.setOnValueChangedListener(this);
   minutePicker.setOnScrollListener(this);
   minutePicker.setMaxValue(60);
   minutePicker.setMinValue(0);
   minutePicker.setValue(49);
 }

还差一步,Activity需要继承一下OnValueChangeListener,OnScrollListener,Formatter:

public class MainActivity extends Activity implements OnValueChangeListener,OnScrollListener,Formatter{...}

最后说一点就是NumberPicker也是可以显示文字的,重新定义一个NumberPicker,加载一下:

valuepicker = (NumberPicker) findViewById(R.id.valuepicker);
    String[] city = {"立水桥","霍营","回龙观","龙泽","西二旗","上地"};
    valuepicker.setDisplayedValues(city);
    valuepicker.setMinValue(0);
    valuepicker.setMaxValue(city.length - 1);
    valuepicker.setValue(4);

最后显示的效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍Android日期选择器对话框DatePickerDialog使用详解,包括了Android日期选择器对话框DatePickerDialog使用详解的使用技巧和注意事项,需要的朋友参考一下 调用Android原生日期选择器对话框就是DatePickerDialog,具体内容如下 在Android4.4系统上效果如图: 在Android5.0以上效果如图: 1、Activity的onC

  • 本文向大家介绍jQuery选择器之属性筛选选择器用法详解,包括了jQuery选择器之属性筛选选择器用法详解的使用技巧和注意事项,需要的朋友参考一下 在这么多属性选择器中[attr="value"]和[attr*="value"]是最实用的 [attr="value"]能帮我们定位不同类型的元素,特别是表单form元素的操作,比如说input[type="text"],input[type="che

  • 本文向大家介绍Android滚轮选择时间控件使用详解,包括了Android滚轮选择时间控件使用详解的使用技巧和注意事项,需要的朋友参考一下 滚轮选择控件 Android自带的选择时间控件有点丑,往往产品和设计都比较嫌弃,希望做成ios一样的滚轮选择,下面是我在NumberPicker的基础上自定义的选择控件,效果如下: 原理 基于NumberPicker实现 动态填充数值 联动 接口监听回调 实现

  • 本文最初发表于博客园,并在GitHub上持续更新前端的系列文章。欢迎在GitHub上关注我,一起入门和进阶前端。 以下是正文。 CSS3介绍 CSS3在CSS2基础上,增强或新增了许多特性, 弥补了CSS2的众多不足之处,使得Web开发变得更为高效和便捷。 CSS3的现状 浏览器支持程度不够好,有些需要添加私有前缀 移动端支持优于PC端 不断改进中 应用相对广泛 应对的策略:渐进增强 (1)坚持渐

  • 本文向大家介绍jQuery表单选择器用法详解,包括了jQuery表单选择器用法详解的使用技巧和注意事项,需要的朋友参考一下 表单选择器 1. :button Selector   1. jQuery(":button")   2. 选择所有元素和类型为按钮的元素 2. :checkbox Selector   1. jQuery(":checkbox")   2. 选择所有元素和类型为复选框的元素

  • 希望能澄清一下我什么时候应该使用和。这可能不是节奏问题,但也许我错过了一些关于Golang的知识。 对于我认为基本思想是等待通道的下一个输出。不完全确定什么是可以。 例如,在cadence示例中,< code>local_activity链接并粘贴在下面: 我们不使用任何 但是,在这里的例子中,它也使用信号通道:根据外部输入改变优步节奏睡眠时间 我还会将代码粘贴到这里 你可以看到有,我不完全确定它