1

ランナブルに0.75秒ごとにUIの日付を付けないようにしたいのですが、AnsyTaskを使用したくありません。しかし、TextViewはforループの最後にのみ設定されています。理由は何ですか?

...

robotWords = "........Hey hello user!!!";
        wordSize = robotWords.length();
        mHandler.postDelayed(r, 750);
    }

    private Runnable r = new Runnable()
    {
        public void run()
        {
            for(int i=0; i<wordSize; i++)
            {           
                robotTextView.setText("why this words only display on the textView at last operation on this for loop?");
                Log.i(TAG, robotWords.substring(0, i));
                try
                {
                    Thread.sleep(750);
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }

        }
    };
4

3 に答える 3

1

この行のため、TextViewはforループの最後にのみ設定されますThread.sleep(750);

テキストが実際にテキストビューに設定される前に、スレッドはスリープ状態になります。Thread.sleep(750);CountDownTimerを使用または使用する代わりに、750ミリ秒ごとにHandler.postDelayedを呼び出す必要があると思います。

new CountDownTimer(750 * wordSize, 750) {

 public void onTick(long millisUntilFinished) {
     robotTextView.setText("why this words only display on the textView at last operation on this for loop?");
            Log.i(TAG, robotWords.substring(0, i));
 }

 public void onFinish() {         
 }

}。始める();

于 2012-09-05T02:13:13.830 に答える
1

別のスレッドからUIスレッドを呼び出すべきではありません。CountDownTimerを使用する

    new CountDownTimer(wordSize*750, 750) {

         public void onTick(long millisUntilFinished) {
             robotTextView.setText("...");
         }

         public void onFinish() {

         }
    }.start();
于 2012-09-05T02:15:19.450 に答える
1

これを試して、操作を実行するときに「doStuff()」を呼び出します

public void doStuff() {
    new Thread(new Runnable() {
        public void run() {

    for(int i=0; i<wordSize; i++) {           
        robotTextView.setText("why this words only display on the textView at last operation on this for loop?");
        Log.i(TAG, robotWords.substring(0, i));


                robotTextView.post(new Runnable() {
                    public void run() {
                           robotTextView.setText("why this words only display on the textView at last operation on this for loop?");
                    }
                });

        try {
            Thread.sleep(750);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

            }
        }
    }).start();
}

お役に立てれば!

于 2012-09-05T02:34:40.863 に答える