简体   繁体   English

在Android中启动新活动之前,如何在onClickListener中处理UI更改?

[英]How will UI changes be processed in the onClickListener before starting a new activity in Android?

I have been searching here in this forum but I cannot find any answer to my question. 我在此论坛中一直在搜索,但找不到我的问题的任何答案。 I have the following problem: 我有以下问题:

I have a imageview and two texts, and I want to change the color on selection to show show visual feedback before loading the next activity. 我有一个imageview和两个文本,我想更改选择的颜色以在加载下一个活动之前显示视觉反馈。

@Override
public void onClick(View v) {
   openFormOverviewButton.setColorFilter(0x22FFFFFF, Mode.SRC_ATOP);
   openFormOverviewTitle.setTextColor(0xFF00851B);
   openFormOverviewText.setTextColor(0xFF00851B);
   Intent tki = new Intent();
   tki.setClass(getApplication(), DataCollectorFormOverviewActivity.class);
   startActivity(tki);
}

I would expect that the button and the texts are changed and then the new activity is started. 我希望按钮和文本被更改,然后开始新的活动。 However, the text does not change and the activity starts. 但是,文本不会更改,并且活动开始。 If I invoke the three lines in a runnable on the UI thread (runOnUiThread(new Runnable() {...}) then the changes are applied before the activity is started. This is actually strange, because the onClick method is called on the main thread aka. UI thread. 如果我在UI线程的runnable中调用三行代码(runOnUiThread(new Runnable(){...}),那么更改将在活动开始之前应用。这实际上很奇怪,因为onClick方法是在主线程又名UI线程。

Does that mean that changes to the UI thread are not executed imediatelly on the UI thread? 这是否意味着对UI线程的更改不会立即在UI线程上执行? Or am I doing something completely wrong? 还是我做错了什么?

Best, Adam 最好,亚当

Correct. 正确。 Changes to UI elements are not executed immediately. UI元素的更改不会立即执行。 They're executed the next time the UI loop runs, which is after you release control. 它们在下一次UI循环运行时执行,也就是释放控件后。 This is why you can't do expensive operations on the UI thread. 这就是为什么您不能在UI线程上执行昂贵的操作的原因。

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

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