开发者

Refresh a button's label when clicked

I need to change the text of a Button when clicked, so I used myButton.setText("New Label"), but the view is refresh only when the onClick(View v) function is over.

Actually what I need is to change the label when the button is clicked without waiting the end of the function : what I do when the button is clicked takes quite some times.

One solution开发者_JS百科 could be to force the View to refresh before the end of onClick() but I don't know if it is possible or how to do it.

Another solution could be to move what I do in onClick() outside of it in a new function which would be called straight after the end of onClick(), but here again, I don't know how I could do that.


You might want to set an OnTouchListener and implement the onTouch method.

In this method you'll get a MotionEvent and can listen to the MotionEvent.ACTION_DOWN.

So you can change the label as soon as the user touches the button.


You shouldn't do anything time consuming on the UI thread. You should start separate worker thread then update the text on the button and return from onClick function.

For how to start worker thread that should update UI as it progresses and/or completes see AsyncTask.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜