Android - 使用 runOnUiThread 从线程进行 UI 更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/901239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - using runOnUiThread to do UI changes from a thread
提问by lostInTransit
I am using a custom title view and would like to show/hide a progressbar in the title view when a thread is working.
我正在使用自定义标题视图,并希望在线程工作时在标题视图中显示/隐藏进度条。
This is my title view's XML
这是我的标题视图的 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/image_left_btn"
android:layout_width="75dip"
android:layout_height="wrap_content"
android:text="Back"
/>
<TextView
android:id="@+id/image_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dip"
android:textStyle="bold"
android:textColor="#fff"
android:layout_gravity="center"
android:gravity="center"
android:paddingLeft="8dip"
android:paddingRight="8dip"
/>
<ProgressBar
android:id="@+android:id/progress_small_title"
style="?android:attr/progressBarStyleSmall"
android:layout_width="75dip"
android:layout_height="wrap_content"
android:paddingRight="8dip"/>
</LinearLayout>
In my Activity, after setting this as the custom title bar, I do this
在我的活动中,将其设置为自定义标题栏后,我执行此操作
titleProgress = (ProgressBar)findViewById(R.id.progress_small_title);
titleProgress.setVisibility(View.INVISIBLE);
where titleProgress is ProgressBar object.
其中 titleProgress 是 ProgressBar 对象。
This is what I do in my thread
这就是我在我的线程中所做的
runOnUiThread(new Runnable() {
public void run() {
titleProgress.setVisibility(View.VISIBLE);
}
});
//long operation here
runOnUiThread(new Runnable() {
public void run() {
titleProgress.setVisibility(View.INVISIBLE);
}
});
But there is no change to the progress bar. It is never displayed. Can someone please tell me what is wrong with the code?
但是进度条没有变化。它永远不会显示。有人可以告诉我代码有什么问题吗?
Is it possible to display the title progressbar in a custom title?
是否可以在自定义标题中显示标题进度条?
Thanks.
谢谢。
采纳答案by haseman
Few Things to try:
尝试的几件事:
1) (This probably isn't it) Make sure "titleProgress
" is volatile.
1)(可能不是这样)确保“ titleProgress
”是不稳定的。
2) Try throwing a few postInvalidate()
or titleProgress.postInvalidate()
in there to trigger a redraw.
2)尝试扔一些postInvalidate()
或titleProgress.postInvalidate()
在那里触发重绘。
3) Have you sacraficed a x486 machine on an alter resembling a giant green robot? (just kidding)
3) 你有没有在一个类似于巨型绿色机器人的祭坛上牺牲一台 x486 机器?(只是在开玩笑)
Let me know if those first two (and if you're really desperate, the third) get you anywhere.
让我知道前两个(如果你真的很绝望,第三个)是否能帮到你。
回答by Eddified
What you should use in this case is AsyncTask, see http://developer.android.com/reference/android/os/AsyncTask.html
在这种情况下您应该使用的是 AsyncTask,请参阅 http://developer.android.com/reference/android/os/AsyncTask.html
If you're not targeting a pre-1.5 version of Android, you will have to use a different class called UserTask. See http://android-developers.blogspot.com/2009/05/painless-threading.html
如果您的目标不是 1.5 之前的 Android 版本,则必须使用名为 UserTask 的不同类。见http://android-developers.blogspot.com/2009/05/painless-threading.html
回答by Houcine
use Communication with Handlers ,
使用与处理程序的通信,
- execute your thread
- into
run()
: you should send a message to the Handler which will update the progressBar (Horizontal of course ) :handler.sendMessage(handler.obtainMessage());
in your Activity , you should override the method
handleMessage(Message msg )
: like thishandler = new Handler(){ @override public void handleMessage(Message msg ) { //here you write the code which will update your progressBar } };
- 执行你的线程
- 进入
run()
:您应该向处理程序发送一条消息,该消息将更新进度条(当然是水平的):handler.sendMessage(handler.obtainMessage());
在您的 Activity 中,您应该覆盖该方法
handleMessage(Message msg )
:像这样handler = new Handler(){ @override public void handleMessage(Message msg ) { //here you write the code which will update your progressBar } };
回答by Vincent Mimoun-Prat
Is your layout correct? Did you try to get this working with a vertical layout first? Just to see if the progress bar is visible when you start your activity?
你的布局正确吗?您是否尝试首先使用垂直布局进行此操作?只是为了在您开始活动时查看进度条是否可见?
回答by Intrications
Try changing the style of the ProgressBar to horizonal:
尝试将 ProgressBar 的样式更改为水平:
style="?android:attr/progressBarStyleHorizontal"
The original
原本的
style="?android:attr/progressBarStyleSmall"
gives a circular "progressbar" which can't be fully seen in the titlebar.
给出了一个无法在标题栏中完全看到的圆形“进度条”。