簡體   English   中英

如何從線程更新blackberry UI項?

[英]How do I update blackberry UI items from a thread?

public class PlayText extends Thread {

   private int duration;
   private String text;
   private PlayerScreen playerscrn; 

   public PlayText(String text, int duration) {
        this.duration = duration;
        this.text = text;
        this.playerscrn = (PlayerScreen)UiApplication.getUiApplication().getActiveScreen();
   }



    public void run() {
        synchronized(UiApplication.getEventLock()) {
            try{
                RichTextField text1player = new RichTextField(this.text, Field.NON_FOCUSABLE);
                playerscrn.add(text1player);
                playerscrn.invalidate();

                Thread.sleep(this.duration);

                RichTextField text2player = new RichTextField("hahhaha", Field.NON_FOCUSABLE);
                playerscrn.add(text2player);
                playerscrn.invalidate();

                Thread.sleep(1000);
                RichTextField text3player = new RichTextField("Done", Field.NON_FOCUSABLE);
                playerscrn.add(text3player);
                playerscrn.invalidate();

            }catch(Exception e){
             System.out.println("I HAVE AN ERROR");
            }   
        }
    }
}

使用上面的代碼,我正在嘗試創建一個小型文本播放器。
而是逐個獲取所有文本標簽,例如

顯示text1player
等待this.duration毫秒
顯示text2player
等待1000毫秒
顯示text3player
線程完成。

屏幕等待this.duration + 1000毫秒並立即顯示所有標簽。 我嘗試使用runnable並調用.invokeLater或.invokeAndWait,但我仍然得到相同的行為,即使我像上面那樣使用synchronized變臟,它仍然不起作用。
有誰知道我如何一次顯示每個標簽?
謝謝!

嘗試在sleep語句之間移動同步...也許它沒有顯示,因為你已經獲得了鎖定,並且在你睡覺時UI線程無法更新。

當您的線程正在睡眠時,您是否在UI中看到延遲或無響應? 試試這種方式:

public class PlayText extends Thread {

   private int duration;
   private String text;
   private PlayerScreen playerscrn; 

    public PlayText(String text, int duration) {
        this.duration = duration;
        this.text = text;
        this.playerscrn = (PlayerScreen)UiApplication.getUiApplication().getActiveScreen();
    }

    private void displayTextLabel(string textToDisplay){
        synchronized(UiApplication.getEventLock()) {
            playerscrn.add(new RichTextField(textToDisplay, Field.NON_FOCUSABLE));
            playerscrn.invalidate();
        }
    }

    public void run() {

            try{

                displayTextLabel(this.text);
                Thread.sleep(this.duration);

                displayTextLabel("hahhaha");
                Thread.sleep(1000);

                displayTextLabel("Done");

            }catch(Exception e){
             System.out.println("I HAVE AN ERROR");
            }   
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM