【发布时间】: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