簡體   English   中英

當我單擊活動時,我在其中有3個文本視圖。 我想2秒的差距后加載文本視圖一個又一個什么樣的?

[英]When I click on the activity I have 3 Text View in that. I want to load the Text View one after another like after a gap of 2 seconds?

我不確定,但是我的研究促使我得出了這一點。 我想我需要創建Thread對象,然后可以使用Thread.sleep(seconds); 但是我不確定它如何與文本視圖一起使用。

  private void runThread() {

    new Thread() {
        public void run() {
            int i = 0;
            while (i++ < 2) {
                try {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            TextView tv1 = (TextView) findViewById(R.id.tvFirst);
                            TextView tv2 = (TextView) findViewById(R.id.tvSecond);
                            TextView tv3 = (TextView) findViewById(R.id.tvThird);
                        }
                    });
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
}

這甚至沒有回應。 我知道我在這里做錯了,但這就是我現在能想到的。 任何幫助將不勝感激。

使用Handler.postDelayed 例:

 new Handler().postDelayed(new Runnable(){
    @Override
     public void run(){
         runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        TextView tv2 = (TextView) findViewById(R.id.tvSecond);
                        tv2.setText("I set this text after waiting for 2 seconds");

                    }
                });

      }
 },2000);

兩秒鍾后,這將更新您的第二個textView。 您可以與其他人做幾乎相同的事情。

暫無
暫無

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

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