質問

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?

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);

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?

役に立ちましたか?

解決

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;
}

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);

That did the trick for me!

他のヒント

My answer is to improve on the basis of Alan Moore, just change the last line

    input.setInputType(InputType.TYPE_CLASS_NUMBER); 

to

    input.setRawInputType(InputType.TYPE_CLASS_NUMBER);

then there will no more problem like personne3000 said, no more crashes. I'm sorry my English is not good, but I hope you can understand what I mean.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top