前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发之滑动数值选择器NumberPicker用法示例

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

作者头像
砸漏
发布2020-11-04 09:52:20
1.2K0
发布2020-11-04 09:52:20
举报
文章被收录于专栏:恩蓝脚本

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

简介:

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

实际效果:

常用方法:

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

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

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

在布局文件中调用:

代码语言:javascript
复制
<?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 改变或现时的数值)

具体实现方法:

代码语言:javascript
复制
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相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档