0

私のアプリケーションは質問/回答アプリケーションであり、ユーザーが質問をし、サーバーがユーザーに質問を送信します。ユーザーが質問を受信すると、ShowQuestionボタンが表示されます。ユーザーがクリックすると、タイマーを開始します。たった36秒で答えるために私はこのように私のxmlでtextViewを構築します

<TextView
        android:id="@+id/tvRemaingTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible" 
/>

私のJavaアクティビティでは、これを作成します

TextView remaingTimer;
CountDownTimer timer;
private void initialize() {
remaingTimer=(TextView)findViewById(R.id.tvRemaingTime);
}

ユーザーがShowQuestionをクリックすると、これを作成します

timer =new CountDownTimer(360000,100) {
   public void onTick(long millisUntilFinished) {
        remaingTimer.setText(millisUntilFinished+"");
   }
   public void onFinish() {
    remaingTimer.setText("finish");
   }
};
timer.start();

しかし、それは何も印刷しません、私は何を間違っているのですか?

ノート

私はAsyncTaskを使用して、次のようにサーバーから質問を取得しています。

public class getQuestionFromServer extends
    AsyncTask<String, Integer, String[]> {}

ただし、ShowQuestionボタンが表示されないため、textViewには影響しません。それ以外の場合、ユーザーはサーバーから質問を受け取ります。

4

1 に答える 1

1

runOnUiThreadを使用して、スレッドからTextViewを次のように更新できます。

TextView remaingTimer;
CountDownTimer timer;
private boolean mClockRunning=false;
private int millisUntilFinished=36;
private void initialize() {
remaingTimer=(TextView)findViewById(R.id.tvRemaingTime);
}
 ShowQuestionbutn.setOnClickListener(new OnClickListener() {    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(mClockRunning==false)
                {
                    mClockRunning=true;
                    millisUntilFinished=0;
                    myThread();
                }
    public void myThread(){
            Thread th=new Thread(){

             @Override
             public void run(){
              try
              {

               while(mClockRunning)
               {
               Thread.sleep(100L);// set time here for refresh time in textview
               CountDownTimerActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                 // TODO Auto-generated method stub
                     if(mClockRunning)
                     {
                                                                                                       if(millisUntilFinished<0)
               {
               mClockRunning=false;
               millisUntilFinished=36;
                }
                else
                {
               millisUntilFinished--;
               remaingTimer.setText(millisUntilFinished+"");//update textview here
               }
                     }

            };
                       }
              }catch (InterruptedException e) {
            // TODO: handle exception
             }
             }
            };
            th.start();
           }
于 2012-06-23T07:31:17.513 に答える