简体   繁体   English

禁用时NumberPicker不会变灰

[英]NumberPicker not graying out when disabled

I believe I may have found a bug in the Android UI, but I'd like to make sure I'm not doing something flat out wrong. 我相信我可能在Android用户界面中发现了一个错误,但我想确保自己没有做错任何事情。 Here is a simplified version of the code I'm running that's causing the problem. 这是导致问题的我正在运行的代码的简化版本。 This listener is set on a specific NumberPicker in my UI, and it properly disables / enables things, but if the user has changed the value of one of the other NumberPickers that it disables, it behaves a little bit oddly. 该侦听器是在我的UI中的特定NumberPicker上设置的,它可以正确禁用/启用某些功能,但是如果用户更改了其禁用的其他NumberPickers之一的值,它的行为会有些奇怪。

It still properly disables the input, but it fails to grey the value out, making it look like it's still enabled. 它仍然可以正确禁用输入,但是无法将值显示为灰色,使其看起来仍然处于启用状态。 Is this intended? 这是故意的吗? Am I doing something wrong? 难道我做错了什么?

NumberPicker.OnValueChangeListener diceChangeListener = new NumberPicker.OnValueChangeListener() {
    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        View parent = (View) picker.getParent();    

        if (newVal == 0) {
            ((NumberPicker) parent.findViewById(R.id.diceCountPicker1)).setEnabled(false);
        } else if (oldVal == 0) {
            ((NumberPicker) parent.findViewById(R.id.diceCountPicker1)).setEnabled(true);
        }
    }
};

Let me know if there's a better way to do this, or if this is a legitimate bug. 让我知道是否有更好的方法可以做到这一点,或者这是否是合法的错误。 If it is a bug, is there a way to work around it? 如果是错误,是否有解决方法?

Edit: Here's the definition of diceCountPicker1 in the XML: 编辑:这是XML中diceCountPicker1的定义:

<NumberPicker
    android:id="@+id/diceCountPicker1"
    android:layout_width="48dp"
    android:layout_height="128dp"
    android:descendantFocusability="blocksDescendants"
    />

Edit 2: 编辑2:

I have tested this in emulators, and confirmed that the problem doesn't exist before Jellybean (4.1). 我已经在仿真器中对此进行了测试,并确认在Jellybean(4.1)之前该问题不存在。 It works properly in ICS. 它在ICS中正常工作。 That's... annoying. 真讨厌 But I may be able to live with it for now. 但我现在也许可以忍受。 I'll leave this open for potential ways to work around the bug, but it looks to me like a real bug, and I doubt it can be fixed. 我将为解决该bug的潜在方法留出余地,但是在我看来,它像是一个真正的bug,我怀疑它是否可以解决。

Well, with no additional feedback coming in on this, I'm going to go ahead and close it out. 好吧,在此没有其他反馈,我将继续进行讨论。 Here's what I know: 这是我所知道的:

This appears to be a bug introduced in Jellybean. 这似乎是Jellybean中引入的错误。 I have tested in emulators, and the UI works as expected in ICS and earlier. 我已经在模拟器中进行了测试,并且UI可以在ICS和更早版本中按预期工作。 For the time being, I'm just going to deal with this, and attempt to submit an official bug report. 目前,我将要处理此问题,并尝试提交正式的错误报告。

Try in this way. 以这种方式尝试。

        NumberPicker npicker = (NumberPicker) findViewById(R.id.diceCountPicker1);
        npicker.setMaxValue(100);
        npicker.setMinValue(0);

        npicker.setOnValueChangedListener(new OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal,
                    int newVal) {
                // Conditions for Enable/Disable picker                 
                if (newVal == 0) {
                    picker.setEnabled(false);
                } else if (oldVal == 0) {
                    picker.setEnabled(true);
                }
            }
        });

It works well for me and it is behaving properly with Enable/Disable as per added conditions. 它对我来说效果很好,并且根据添加的条件,它具有启用/禁用的功能。

Thanks. 谢谢。

Setting android:enabled="false" in NumberPicker and DatePicker xml layouts doesn't gray-out the controls for me on Android 4.0.4 device. 在NumberPicker和DatePicker xml布局中设置android:enabled="false"不会对我在Android 4.0.4设备上的控件变灰。 However doing so programmetically via setEnabled(false) works as expected. 但是,通过setEnabled(false)以编程方式进行此操作可以按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM