Android NumberPicker上的setMinValue未选择正确的最小值
我想创建一个带有显示值数组的NumberPicker。当显示NumberPicker时,值不应更改,但最小值和最大值会根据用户操作动态更改 代码如下:Android NumberPicker上的setMinValue未选择正确的最小值,android,android-number-picker,Android,Android Number Picker,我想创建一个带有显示值数组的NumberPicker。当显示NumberPicker时,值不应更改,但最小值和最大值会根据用户操作动态更改 代码如下: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<NumberPicker
android:id="@+id/picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
当我设置最大值而不设置最小值时,我以显示的值结束,该值包括0到7之间的值。如果我将minValue设置为0,则具有相同的行为。
但如果我将minValue设置为3,则显示的值在0到4之间。当它应该在3到7之间的时候
我不明白为什么。我是否做错了什么,或者该组件是否存在问题?可以从中进行解释
设置“最小”和“最大”时,可以定义可以使用NumberPicker输入的int值。因此setValueint只接受从最小到最大的值
显示值为标签覆盖。它将以下一种方式使用——NumberPicker将使用索引值为-min的标签。可以从中进行解释
设置“最小”和“最大”时,可以定义可以使用NumberPicker输入的int值。因此setValueint只接受从最小到最大的值
显示值为标签覆盖。它将以下一种方式使用——NumberPicker将使用索引值为-min的标签。如果您查看setMinValue或setMaxValue的实现,则会看到一条注释: 因此,要解决这个问题,当您想要更新显示的值时,最好只设置值列表
//do this once in onCreate and save the values list somewhere where you have access
final List<String> values = new ArrayList<>();
for (int i = 0; i < 10; ++i) {
values.add(String.valueOf(i));
}
NumberPicker picker = (NumberPicker) findViewById(R.id.picker);
picker.setWrapSelectorWheel(false);
//do this every time you want to change the displayed values and set minValue and maxValue accordingly
//also make sure that you stay within the bounds of value
int minValue = 3;
int maxValue = 7;
final List<String> subValues = values.subList(minValue, maxValue + 1);
String[] subValuesArray = new String[subValues.size()];
subValuesArray = subValues.toArray(subValuesArray);
picker.setDisplayedValues(subValuesArray);
picker.setMinValue(0);
picker.setMaxValue(subValues.size() - 1);
如果您查看setMinValue或setMaxValue的实现,会有一条评论说: 因此,要解决这个问题,当您想要更新显示的值时,最好只设置值列表
//do this once in onCreate and save the values list somewhere where you have access
final List<String> values = new ArrayList<>();
for (int i = 0; i < 10; ++i) {
values.add(String.valueOf(i));
}
NumberPicker picker = (NumberPicker) findViewById(R.id.picker);
picker.setWrapSelectorWheel(false);
//do this every time you want to change the displayed values and set minValue and maxValue accordingly
//also make sure that you stay within the bounds of value
int minValue = 3;
int maxValue = 7;
final List<String> subValues = values.subList(minValue, maxValue + 1);
String[] subValuesArray = new String[subValues.size()];
subValuesArray = subValues.toArray(subValuesArray);
picker.setDisplayedValues(subValuesArray);
picker.setMinValue(0);
picker.setMaxValue(subValues.size() - 1);
为了简化@Bmuig answer,只需一个辅助函数:
private void configurePicker(NumberPicker picker, ArrayList<String> values, int minValue, int maxValue, int currentValue) {
picker.setDisplayedValues(values) //to avoid out of bounds
picker.setMinValue(minValue);
picker.setMaxValue(maxValue);
picker.setValue(currentValue);
picker.setDisplayedValues((String[]) values.subList(minValue, maxValue + 1).toArray());
}
为了简化@Bmuig answer,只需一个辅助函数:
private void configurePicker(NumberPicker picker, ArrayList<String> values, int minValue, int maxValue, int currentValue) {
picker.setDisplayedValues(values) //to avoid out of bounds
picker.setMinValue(minValue);
picker.setMaxValue(maxValue);
picker.setValue(currentValue);
picker.setDisplayedValues((String[]) values.subList(minValue, maxValue + 1).toArray());
}
那么,这是否意味着每次我更改最小值和最大值时都必须更改显示值的数组?不幸的是,是的,你必须这样做,这是否意味着每次我更改最小值和最大值时都必须更改显示值的数组?不幸的是,你必须这样做,请详细说明?为什么它不能解决OP的问题?你不是也建议只要最小/最大值改变,显示值就应该改变吗?也许我误解了?对不起,我误解了你的建议。它按预期工作。对不起!你能详细说明一下吗?为什么它不能解决OP的问题?你不是也建议只要最小/最大值改变,显示值就应该改变吗?也许我误解了?对不起,我误解了你的建议。它按预期工作。对不起!