0

I want to do something very simple. I have a progress-bar and a textView that I want to dynamically update on a button click, but more than once during the function call.

I want it to change to 'A' then wait a second, then switch to 'B' ... for example.

I understand that my function blocks the UI/main thread, which is what if I setText() ten times, I only see the tenth text appear when the function completes.

So I've tried handlers/runnables in the UI thread, invalidating (and postInvalidating) to encourage refreshing and runOnUiThread() and they all just update when they've finished running leaving me only with the final state.

public void requestPoint(View v) {
    textProgress.setText("Requesting A Point");
    waiting(200);
    progressBar.setProgress(5);
    textProgress.setText("Request Received!");
    waiting(500);
    ....
}

I think I've turned nearly all the similar question links purple and wasted way too much time on this simple thing (although I did learn a lot about threading on Android) but I feel I must be missing something crucial, this doesn't seem like that hard of task.

Any help would be greatly appreciated.

4

1 回答 1

0

假设您真的只是想在等待方法上浪费时间。这是假设 200 毫秒延迟,然后是 500 毫秒延迟,如您的示例中所示。

public void requestPoint(View v) {
    textProgress.setText("Requesting A Point");
    handler.postDelayed(new Runnable(){
        public void run(){ 
            progressBar.setProgress(5);
            textProgress.setText("Request Received!");
        }
    }, 200);

    handler.postDelayed(new Runnable(){
        public void run(){ 
            ....
        }
    }, 700);
....
}
于 2013-08-26T17:40:01.080 回答