如何用动画更改NumberPicker的值?

Meh*_*ran 9 animation android android-animation numberpicker

我已经为Android创建了一个应用程序,其中有一个NumberPicker.我需要更改此NumberPicker的值,但需要使用平滑动画,就像触摸它并更改其值一样.

例如,假设当前值为1并且它将为5,我希望NumberPicker从1旋转到2然后旋转到3,依此类推.但我希望它不仅可以瞬间改变价值!

如果我使用以下代码:

numberPicker.setValue(5);
Run Code Online (Sandbox Code Playgroud)

它的值会立即变为5,但我希望它从1到5卷起,就像你手动触摸并旋转它一样.

pas*_*ssy 15

我通过refelction解决了它

        /**
         * using reflection to change the value because
         * changeValueByOne is a private function and setValue
         * doesn't call the onValueChange listener.
         * 
         * @param higherPicker
         *            the higher picker
         * @param increment
         *            the increment
         */
        private void changeValueByOne(final NumberPicker higherPicker, final boolean increment) {

            Method method;
            try {
                // refelction call for
                // higherPicker.changeValueByOne(true);
                method = higherPicker.getClass().getDeclaredMethod("changeValueByOne", boolean.class);
                method.setAccessible(true);
                method.invoke(higherPicker, increment);

            } catch (final NoSuchMethodException e) {
                e.printStackTrace();
            } catch (final IllegalArgumentException e) {
                e.printStackTrace();
            } catch (final IllegalAccessException e) {
                e.printStackTrace();
            } catch (final InvocationTargetException e) {
                e.printStackTrace();
            }
        }
Run Code Online (Sandbox Code Playgroud)

工作完美.我不知道为什么这种方法是私密的

  • 这为我吐出了NoSuchMethodException.我正在使用扩展NumberPicker的类. (2认同)