我创建了这个应用程序,现在我想使用textview显示其他活动开始之前的秒数,但我不知道如何,我在倒数计时器内创建了一个txtview,但它从未显示
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
}
};
答案 0 :(得分: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();