2013-03-25 43 views
0

我创造了这个应用程序,现在我想用一个TextView来显示秒,直到其他活动开始,但我不知道怎么回事,我创建了一个txtview的countdowntimer内,但它从来没有表现出Countdowntimer的TextView

Event=new String(Edt.getText().toString()); 
final int time = Integer.parseInt(sec.getText().toString()); 

Intent myInt = new Intent(MainActivity.this,Receiver.class); 

myInt.putExtra("key",Event); 
endingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,2,myInt,PendingIntent.FLAG_CANCEL_CURRENT); 
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(time*1000),pendingIntent); 

new CountDownTimer(time*1000, 1000) { 

    @Override 
    public void onTick(long millisUntilFinished) { 
    // TODO Auto-generated method stub 
     txtV.setText("Activity starts"+millisUntilFinished/1000+"seconds"); // here is the txtV which isn't shown 
    } 

    @Override 
    public void onFinish() { 
     // TODO Auto-generated method stub 

    } 
}; 
+5

如何启动一个'CountDownTimer'? – 2013-03-25 13:11:01

回答

1

首先,您需要通过拨打电话start method

启动计数器但是要小心,您只能从创建此视图的线程更改视图。你可以这样做的一种方法是:post runnable on view

CountDownTimer timer = new CountDownTimer(time*1000, 1000) { 

     @Override 
     public void onTick(long millisUntilFinished) { 
      txtV.post(new Runnable() { 
       @Override 
       public void run() { 
          txtV.setText("Activity starts"+millisUntilFinished/1000+"seconds"); // here is the txtV which isn't shown 
       } 
      }); 
     } 

     @Override 
     public void onFinish() { 
      // TODO Auto-generated method stub 

     } 
    }; 
    timer.start(); 
+0

谢谢,这是非常有益的! – Marques 2013-03-25 13:33:32

+0

@Marques,如果问题解决了,你可以接受答案 – 2013-03-25 13:56:01