我已经查看了许多关于如何以编程方式更改数字选择器的线程,并触发了OnValueChangedListener。他们都没有为我工作。我需要的是数字选择器跳过某些数字,如果他们已被带到一个空闲位置。我目前的“解决方案”是:
// Set up channel picker change event to update other items in the form
mPickerAddress.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
// If the user scrolls the number picker, update the other form items
// Check to see if this channel is already used, and inform user, and reset picker
int n;
boolean contained = false;
// Iterate over each custom value to see if channel is used
for (n = 0; n < 10; n++) {
if (mCustomValues[n][0] == i1) {
contained = true;
}
}
if (contained) {
// Check direction being moved, if moving positive, skip over that way and vice versa
// If positive...
if(i1 - i > 0){
pickerSkipper(mPickerAddress,true);
}
else{
pickerSkipper(mPickerAddress,false);
}
} else {
// Update address value
mCustomValues[mPickerChannel.getValue() - 1][mCustomAddress] = i1;
// Update has changed
hasChanged[mPickerChannel.getValue() - 1] = true;
}
}
});
跳过方法(从此问题的其他帖子派生的代码):
// Skips forward or backward if a value is already taken
public void pickerSkipper(NumberPicker picker, boolean skip){
Method method;
try {
// reflection call for
// higherPicker.changeValueByOne(true);
method = picker.getClass().getDeclaredMethod("changeValueByOne", boolean.class);
method.setAccessible(true);
if(skip)method.invoke(picker, true);
else method.invoke(picker, false);
} catch (final NoSuchMethodException e) {
e.printStackTrace();
} catch (final IllegalArgumentException e) {
e.printStackTrace();
} catch (final IllegalAccessException e) {
e.printStackTrace();
} catch (final InvocationTargetException e) {
e.printStackTrace();
}
}
配置方法反射的这种或任何其他方式导致程序最终崩溃到无休止的垃圾收集例程中并且“挂起所有线程:X.XXXms”错误。
此时我不需要使用这种方式,我只想要一些有用的东西。让数字选择器向前跳过是不是很难,或者可以吗?
提前谢谢。