【问题标题】:Restricting Android NumberPicker to Numeric Keyboard for Numeric Input (not Alpha keyboard)将 Android NumberPicker 限制为用于数字输入的数字键盘(不是 Alpha 键盘)
【发布时间】:2013-09-18 14:39:19
【问题描述】:

有没有办法在选择 NumberPicker 时建议或限制键盘输入,以便在输入值时只显示数字控件,类似于您如何将 android:inputType="number" 与 EditText 一起使用?

我有一系列值,从 0.0 到 100.0,增量为 0.1,我希望能够在 Android 4.3 中使用 NumberPicker 进行选择。为了让数字可选,我创建了一个与这些值对应的字符串数组,如下所示:

    NumberPicker np = (NumberPicker) rootView.findViewById(R.id.programmingNumberPicker);        
    int numberOfIntensityOptions = 1001;

    BigDecimal[] intensityDecimals = new BigDecimal[numberOfIntensityOptions];
    for(int i = 0; i < intensityDecimals.length; i++ )
    {
        // Gets exact representations of 0.1, 0.2, 0.3 ... 99.9, 100.0
        intensityDecimals[i]  = BigDecimal.valueOf(i).divide(BigDecimal.TEN);
    }

    intensityStrings = new String[numberOfIntensityOptions];
    for(int i = 0; i < intensityDecimals.length; i ++)
    {
        intensityStrings[i] = intensityDecimals[i].toString();
    }

    // this will allow a user to select numbers, and bring up a full keyboard. Alphabetic keys are
    // ignored - Can I somehow change the keyboard for this control to suggest to use *only* a number keyboard
    // to make it much more intuitive? 
    np.setMinValue(0);
    np.setMaxValue(intensityStrings.length-1);
    np.setDisplayedValues(intensityStrings);
    np.setWrapSelectorWheel(false);

作为更多信息,我注意到如果我不使用setDisplayedValues() 方法而是直接设置整数,将使用数字键盘,但这里的问题是数字输入的数量是应有数量的 10 倍 - 例如如果您在控件中输入“15”,则其解释为“1.5”

        // This will allow a user to select using a number keyboard, but input needs to be 10x more than it should be. 
        np.setMinValue(0);
        np.setMaxValue(numberOfIntensityOptions-1);
        np.setFormatter(new NumberPicker.Formatter() {
            @Override
            public String format(int value) {
                return BigDecimal.valueOf(value).divide(BigDecimal.TEN).toString();
            }
        });

关于如何提高数字键盘以允许用户输入这样的十进制数字的任何建议?

【问题讨论】:

  • 很好的问题,你有没有想过这个问题?我也想知道怎么做!
  • @AlanMoore 不幸的是,不,我还没有弄清楚如何做到这一点。
  • 您是否尝试过搜索子编辑文本视图,然后使用上面的更改来获取您的键盘?

标签: android android-softkeyboard numberpicker


【解决方案1】:

我已经成功地做到了这一点,从@LuksProg's helpful answer 大量借用另一个问题。基本思想是搜索 NumberPicker 的 EditText 组件,然后将输入类型指定为数字。首先添加这个方法(再次感谢@LuksProg):

private EditText findInput(ViewGroup np) {
    int count = np.getChildCount();
    for (int i = 0; i < count; i++) {
        final View child = np.getChildAt(i);
        if (child instanceof ViewGroup) {
            findInput((ViewGroup) child);
        } else if (child instanceof EditText) {
            return (EditText) child;
        }
    }
    return null;
}

然后,在我的 Activity 的 onCreate() 方法中,我调用它并设置输入类型:

np.setMinValue(0);
np.setMaxValue(intensityStrings.length-1);
EditText input = findInput(np);    
input.setInputType(InputType.TYPE_CLASS_NUMBER);

这对我有用!

【讨论】:

  • 如果您的 setDisplayedValues() 是类似数字的字符串,则适用于 Android 6,但如果它们包含其他字符(例如,“1 米”、“2 米”等单位),则会崩溃。
【解决方案2】:

我的答案是在Alan Moore的基础上改进,只改最后一行

    input.setInputType(InputType.TYPE_CLASS_NUMBER); 

    input.setRawInputType(InputType.TYPE_CLASS_NUMBER);

那么就不会再像 personne3000 所说的那样出现问题,不会再出现崩溃。 很抱歉我的英语不好,但我希望你能明白我的意思。

【讨论】:

    猜你喜欢
    • 2015-03-28
    • 2011-11-16
    • 2013-09-14
    • 1970-01-01
    • 2014-05-07
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    相关资源
    最近更新 更多