温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中滑动数值选择器NumberPicker的用法分析

发布时间:2020-08-10 09:50:42 来源:亿速云 阅读:526 作者:小新 栏目:移动开发

这篇文章将为大家详细讲解有关Android中滑动数值选择器NumberPicker的用法分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

简介:

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

实际效果:

Android中滑动数值选择器NumberPicker的用法分析

常用方法:

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

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

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

在布局文件中调用:

<&#63;xml version="1.0" encoding="utf-8" &#63;>
<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中滑动数值选择器NumberPicker的用法分析就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI