美文网首页
数值选择器(NumberPicker)的功能与用法

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

作者: babybus_hentai | 来源:发表于2016-06-29 10:54 被阅读960次

安卓原生自带控件
setMinValue(int minVal):设置该组件支持的最小值。
setMaxValue(int maxVal):设置该组件支持的最大值。
setValue(int value):设置该组件的当前值。

布局文件如下:

<?xml version="1.0" encoding="utf-8"?><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" android:background="@color/red_dark" > <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="80dp" 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:visibility="invisible" android:id="@+id/minuteicker" android:layout_width="40dp" android:layout_height="wrap_content" /> <TextView android:visibility="invisible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="秒" /> </LinearLayout></LinearLayout>

java代码

new一个控件对象:
protected NumberPicker hourPicker;

对布局中的控件进行初始化操作
hourPicker =(NumberPicker) findViewById(R.id.hourpicker);hourPickerinit = hourPicker;hourPicker.setFormatter(this);hourPicker.setOnValueChangedListener(this);hourPicker.setOnScrollListener(this);hourPicker.setMaxValue(60);int timenum = SpUtil.getInt( Constant.INIT_MIN,5);hourPicker.setMinValue(1);hourPicker.setValue(timenum);Log.e("TimeSetInitActivity","setValue--onResume");

对其监听:
public void onScrollStateChange(NumberPicker view, int scrollState) { switch (scrollState) { case OnScrollListener.SCROLL_STATE_FLING: break; case OnScrollListener.SCROLL_STATE_IDLE: hourPickerinit.setValue(hourPickerinit.getValue()); break; case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: break; }}

public void onValueChange(NumberPicker picker, int oldVal, int newVal) { ToastUtil.toastShort(hourPickerinit.getValue()+"---onValueChange"); ToastUtil.toastShort("---onValueChange");}

整合即可使用,numberpicker还可用于字符串的滚动,类似于城市滚动选择等

相关文章

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

    安卓原生自带控件setMinValue(int minVal):设置该组件支持的最小值。setMaxValue(i...

  • 数值选择器(NumberPicker)使用

    目录 NumberPicker 数值选择器. 使用其上下旋转的方式选择数值. 默认选择数值,可以设定最大值和最小值...

  • 数值选择器NumberPicker

    数值选择器是用于让用户输入数值,下面有三个方法:setMinValue(int minVal):设置该组件的最小值...

  • 24_简单自定义NumberPicker

    使用默认数字选择器 NumberPicker 步骤 在布局文件中添加 NumberPicker 控件 在代码中设置...

  • NumberPicker设置字符格式化时第一次不起效

    用了NumberPicker来实现一个选择器的时候发现一个问题,选择器中的文本是可以被自己定制的,然后当选择器第一...

  • 常用选择器

    元素选择器 id选择器 id选择器的用法 类选择器 class用法 选择器分组(并集选择器) 并集选择器用法 通配...

  • NumberPicker ——数字选择器

    最近需要做一个数字选择器,找了下没有合适的,就自己写了个。先上效果图 代码很简单,自定义一个HorizontalS...

  • Excle常用函数

    常用加减乘除用法公式 =数值1+数值2=数值1-数值2=数值1*数值2=数值1/数值2 常用函数及用法 sum 求...

  • 常用选择器

    1.id选择器 2.id用法 3.类选择器 4.class用法 5.选择器分组(并集选择器) 6.并集选择器用法 ...

  • 常用选择器

    1.元素选择器 2.id选择器 3.id用法 4.类选择器 5.class用法 6.选择器分组(并集选择器) 7....

网友评论

      本文标题:数值选择器(NumberPicker)的功能与用法

      本文链接:https://www.haomeiwen.com/subject/wpwndttx.html