如何创建一个负数选择器 [英] How to create a negative NumberPicker

查看:116
本文介绍了如何创建一个负数选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道使用Android的默认数字选择器允许负数的简单方法吗?我知道是InputFilter不允许这样做的,但是有什么简单的方法可以覆盖而不重写整个窗口小部件?

Does anyone know of an easy way to allow negative numbers with Android's default numberpicker? I understand that it's the InputFilter that disallows this, but is there any easy way to override without rewriting the whole widget?

推荐答案

更通用,更优雅的解决方案是使用

A more generic and elegant solution is to use NumberPicker.Formatter and use only positive numbers in the NumberPicker.

例如,如果我想在[-50,50]中选择一个数字:

Example if I want to select a number in [-50, 50]:

final int minValue = -50
final int maxValue = 50
NumberPicker numberPicker = new NumberPicker(myActivity);
numberPicker.setMinValue(0);
numberPicker.setMaxValue(maxValue - minValue);
numberPicker.setValue(myCurrentValue - minValue);
numberPicker.setFormatter(new NumberPicker.Formatter() {
    @Override
    public String format(int index) {
        return Integer.toString(index + minValue);
    }
});

然后返回所选值:

int myNewValue = numberPicker.getValue() + minValue

这篇关于如何创建一个负数选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆