gpt4 book ai didi

android - 将 Android NumberPicker 限制为用于数字输入的数字键盘(不是字母键盘)

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:32 24 4
gpt4 key购买 nike

有没有办法在选择 NumberPicker 时建议或限制键盘输入,以便在输入值时仅显示数字控件,类似于如何使用 android:inputType="number"编辑文本?

我有一系列值,从 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();
}
});

关于如何提高数字键盘以允许用户像这样输入十进制数字有什么建议吗?

最佳答案

我已经成功地实现了这一点,大量借鉴了 @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);

这对我有用!

关于android - 将 Android NumberPicker 限制为用于数字输入的数字键盘(不是字母键盘),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18794265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com