前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数值选择器(NumberPicker)使用

数值选择器(NumberPicker)使用

作者头像
李小白是一只喵
发布2020-04-24 08:35:57
1.1K0
发布2020-04-24 08:35:57
举报
文章被收录于专栏:算法微时光

image.png

目录

NumberPicker

数值选择器. 使用其上下旋转的方式选择数值.

默认选择数值,可以设定最大值和最小值.以及字体的颜色.

使用方式:

代码语言:javascript
复制
    <NumberPicker
        android:id="@+id/numberpicker"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:focusable="true"
        android:focusableInTouchMode="true"/>

image.png

实战

activity_main.xml文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <NumberPicker
        android:id="@+id/numberpicker"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:focusable="true"
        android:focusableInTouchMode="true"/>

</android.support.constraint.ConstraintLayout>

代码:

代码语言:javascript
复制
package com.example.user.numberpicker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.NumberPicker;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NumberPicker numberPicker = findViewById(R.id.numberpicker);
        //设置最大值
        numberPicker.setMaxValue(80);
        //设置最小值
        numberPicker.setMinValue(60);
        //设置当前值
        numberPicker.setValue(65);
        //设置滑动监听
        numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            //当NunberPicker的值发生改变时,将会激发该方法
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                String toast = "oldVal:" + oldVal + "   newVal:" + newVal;
                Toast.makeText(MainActivity.this, toast, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

执行效果:

image.png

参考

Android NumberPicker的基本用法及常见问题汇总

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

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

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

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

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