繁体   English   中英

countdowntimer不在textview上打印

[英]countdowntimer doesn't print on textview

我的应用程序是问题/答案应用程序,用户提出问题,然后服务器向用户发送问题,当用户收到问题时,出现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[]> {}

但是我不认为这会对textView产生影响,因为ShowQuestion按钮不会出现,否则用户会从服务器收到问题

您可以使用runOnUiThread从Thread更新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();
           }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM