简体   繁体   English

setEnabled 在 Android 中的某些设备上不起作用

[英]setEnabled not working on some devices in Android

I have a button on my screen that is dynamically enabled or disabled when user make a few actions.我的屏幕上有一个按钮,当用户执行一些操作时,该按钮会动态启用或禁用。 Here is the code that changes button status:这是更改按钮状态的代码:

    private void setContinueButtonStatus() {
        boolean status = false;

        // Every time user selects a ticket this is increased
        for (TicketSpecification specification : ticketSpecificationList.specifications) {
            if (specification.getMyCount() > 0) {
                status = true;
                break;
            }
        }

        findViewById(R.id.btnContinue).setEnabled(status);
    }

My button xml is:我的按钮 xml 是:

    <Button
        android:id="@+id/btnContinue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="@string/continue_button"
        android:layout_alignParentBottom="true"/>

My problem is that this code works fine in Moto G5S, making button enabled or not after user interaction.我的问题是此代码在 Moto G5S 中运行良好,在用户交互后启用或不启用按钮。

But on Galaxy S9 and Android Emulator (I'm using Pixel XL) button keeps disabled, even after user click.但是在 Galaxy S9 和 Android Emulator(我使用的是 Pixel XL)上,即使在用户点击之后,按钮仍然处于禁用状态。 But, if I click on button it become enabled and performs the click normally.但是,如果我点击按钮,它会被启用并正常执行点击。

Anyone knows if this is a bug on Android, or am I making something wrong?任何人都知道这是Android上的错误,还是我做错了什么?

I´ve found the bug here.我在这里找到了错误。

I was using an external lib to manage some stuff on my app, and this lib was returning the info needed to update button on a thread that wasn't main thread.我正在使用外部库来管理我的应用程序上的一些东西,并且这个库返回更新不是主线程的线程上的按钮所需的信息。

I´ve fixed the problem by redirecting my code to main thread.我已经通过将我的代码重定向到主线程来解决这个问题。

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

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