2

変更可能なテキストで1から100を表示したいと思います。私はsleep()関数を使用して、1から100に増加しているように見せることが好きです。私のコードは

for(int i= 0;i<100;i++) {
    scorelevel.setText(String.valueOf(i));
    try{
        Thread.sleep(1000);
    }catch (InterruptedException e) {
        e.printStackTrace();
    }
}

しかし、それは正しく表示されませんでした。任意の助けや提案をいただければ幸いです。

4

4 に答える 4

5

UI スレッドをブロックせず、AsyncTask代わりに使用する

于 2012-06-25T11:07:42.703 に答える
0

TimerTaskリンク)も使用できます。

于 2012-06-25T11:16:25.823 に答える
0

Timerおよびを使用しTimerTaskて、任意の時間ベースのタスクを実行します。

于 2012-06-25T11:12:42.700 に答える
0

runOnUiThreadを使用してカウンターを開始 し、textView を次のように更新できます。

private boolean mClockRunning=false;
private int millisUntilFinished=0;
public void myThread(){
            Thread th=new Thread(){
             @Override
             public void run(){
              try
              {
               while(mClockRunning)
               {
               Thread.sleep(1000L);// set time here for refresh time in textview
               YourCurrentActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                 // TODO Auto-generated method stub
                     if(mClockRunning)
                     {
                     if(millisUntilFinished==100)
               {
               mClockRunning=false;
               millisUntilFinished=0;
                }
                else
                {
               millisUntilFinished++;
               scorelevel.setText(String.valueOf(millisUntilFinished));//update textview here

               }
             }

            };
          }
              }catch (InterruptedException e) {
            // TODO: handle exception
             }
             }
            };
            th.start();
           }
于 2012-06-25T11:13:41.167 に答える