繁体   English   中英

android 中一个线程的 UI 变化

[英]UI changes in a thread in android

我正在编写一个 android 应用程序,该应用程序要求我在再次更改之前按住图像按钮的前景一秒钟。 所以我写了下面的代码(它可以在其他项目的两个按钮上更改文本的 colors)并且我知道问题是我正在更改非主线程中的 UI 元素并且我知道我不能使用“runOnUiThread”方法,但在我目前的 function 中找不到这样做的方法,所以如果有人可以帮助我,请感谢。

public void waitTime(ImageButton imageButton1, ImageButton imageButton2) {
    HandlerThread handlerThread = new HandlerThread("showText");
    handlerThread.start();
    Handler handler = new Handler(handlerThread.getLooper());
    Runnable runnable = () -> {
        imageButton1.setForeground(AppCompatResources.getDrawable(this, R.color.teal_200));
        imageButton2.setForeground(AppCompatResources.getDrawable(this, R.color.teal_200));
    };
    handler.postDelayed(runnable, 1000);
}

您可以使用 View 的postDelayed方法:

Runnable runnable = () -> {
        imageButton1.setForeground(AppCompatResources.getDrawable(this, R.color.teal_200));
        imageButton2.setForeground(AppCompatResources.getDrawable(this, R.color.teal_200));
    };
imageButton1.postDelayed(runnable, 1000);

它将在主线程中更新 UI。

暂无
暂无

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

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