免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
java – 在Android中運行時更新View

這個例子非常簡單:我想通過顯示文本(canvas.drawText())讓用戶知道應(yīng)用程序正在做什么.然后,我的第一條消息出現(xiàn),但不是其他消息.我的意思是,我有一個“setText”方法,但它沒有更新.

onCreate(Bundle bundle) {    super.onCreate(bundle);    setContentView(splash); // splash is the view class    loadResources();    splash.setText("this");    boundWebService();    splash.setText("that"):    etc();    splash.setText("so on");}

視圖的文本繪圖只需在onDraw();中執(zhí)行drawText,因此setText會更改文本但不會顯示它.

有人建議我用SurfaceView替換視圖,但是對于幾個更新來說會有很多麻煩,所以…我怎么能在運行時以動態(tài)更新視圖?

它應(yīng)該很簡單,只顯示2秒的文本,然后主線程做他的東西,然后更新文本…

謝謝!

更新:

我嘗試實現(xiàn)handler.onPost(),但是又重復(fù)了同樣的故事.我給你看一下代碼:

public class ThreadViewTestActivity extends Activity {Thread t;Splash splash;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    splash = new Splash(this);    t = new Thread(splash);    t.start();    splash.setTextow("OA");    try { Thread.sleep(4000); } catch (InterruptedException e) { }    splash.setTextow("LALA");}       }

和:

public class Splash implements Runnable {Activity activity;final Handler myHandler = new Handler();public Splash(Activity activity) {    this.activity=activity;}   @Overridepublic void run() {    // TODO Auto-generated method stub}public synchronized void setTextow(final String textow) {    // Wrap DownloadTask into another Runnable to track the statistics    myHandler.post(new Runnable() {        @Override        public void run() {            TextView t = (TextView)activity.findViewById(R.id.testo);            t.setText(textow);            t.invalidate();         }                       });}}

雖然splash在其他線程中,我在主線程上休眠,我使用處理程序來管理UI和一切,它不會改變一件事,它只顯示最后一次更新.

解決方法:

我還沒有嘗試過,但我認(rèn)為通常的模式是在后臺線程中進行冗長的初始化,并使用Handler.post()來更新UI.有關(guān)不同但可能相關(guān)的示例,請參閱http://developer.android.com/reference/android/widget/ProgressBar.html.

另見this answer,尤其是第一段:

The problem is most likely that you
are running the splash screen (some
sort of Dialog such as ProgressDialog
I assume) in the same thread as all
the work being done. This will keep
the view of the splash screen from
being updated, which can keep it from
even getting displayed to the screen.
You need to display the splash screen,
kick off an instance of AsyncTask to
go download all your data, then hide
the splash screen once the task is
complete.

更新(基于您的更新和評論):除了創(chuàng)建活動的線程之外,您不應(yīng)該更新任何線程中的UI.為什么不能在后臺線程中加載資源?

來源:http://www.icode9.com/content-1-226701.html
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
android程序啟動畫面之Splash總結(jié)
Android Configuration change引發(fā)的問題及解決方法
Android Timer的用法示例
Android 性能優(yōu)化之使用MAT分析內(nèi)存泄露問題
Android防止內(nèi)存溢出淺析
Android中handler的用法總結(jié)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服