限制Android的NumberPicker到数字键盘输入数字(不包括alpha键盘)不包括、键盘、数字、数字键盘

2023-09-07 09:00:23 作者:你若不离我便生死相依

有没有办法选择NumberPicker所以只有数控件输入值时显示的时候建议或限制键盘输入,类似于如何使用安卓inputType =号用的EditText?

Is there a way to suggest or restrict keyboard input when selecting a NumberPicker so only the number controls are shown when entering values, similar to how you can use android:inputType="number" with a EditText?

我有一系列的值,从0.0到100.0增量为0.1,我希望能够用一个NumberPicker中的Andr​​oid 4.3来选择。为了有数字可选择,我已经创建字符串对应于这些值的阵列,如下所示:

I have a series of values, from 0.0 to 100.0 in 0.1 increments that I'd like to be able to use a NumberPicker to select in Android 4.3. In order to have the numbers selectable, I've created an array of strings which corresponds to these values, as shown below:

    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到它的内部preTED为1.5

As more info, I've noticed that if I dont use the setDisplayedValues() method and instead set the integers directly, the numeric keyboard will be used, but the problem here is that the number being entered is 10 times more than it should be - e.g. if you enter "15" into the control its interpreted as "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();
            }
        });

在如何提高一个数字键盘,让用户输入十进制数这样有什么建议?

Any suggestions on how to raise a numeric keyboard to allow a user to enter decimal numbers like this?

推荐答案

我已经成功地做​​到了这一点,从大量举债@LuksProg's有用的答案的另一个问题。基本的想法是要搜索的NumberPicker的的EditText组件,然后向分配输入类型为数字。首先添加这个方法(再次感谢@LuksProg):

I have successfully achieved this, borrowing heavily from @LuksProg's helpful answer to another question. The basic idea is to search for the EditText component of the NumberPicker and then to assign the input type as numeric. First add this method (thanks again to @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;
}

于是,在我的活动的onCreate()方法,我称之为,并设置输入类型:

Then, in my Activity's onCreate() method I call this and set the input type:

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

这奏效了我!

 
精彩推荐
图片推荐