如何重置 countdowntimer 重置activity简历上

查看:3554|回复:1
初级工程师
我在使用CountDownTimer的时候遇到了问题,当我第一次用cancel方法停止CountDownTimer的时候,没有任何问题。可是如果我重启后,想要再停止CountDownTimer的时候,就再也怎么也停止不了。请问是怎么回事? // Main code :
MyCount counter = new MyCount(5);
& && && && &counter.start(); // start timer at 59 seconds
button1.setOnClickListener(new View.OnClickListener() {
& & public void onClick(View v) {
counter.cancel(); // -- & cancelling the timer works here, the clock stops ok
// rest of code snipped
button2.setOnClickListener(new View.OnClickListener() {
& && & public void onClick(View v) {
& && &MyCount counter= new MyCount(countcur,1000); // countcur is the current counter value when last cancelled
& && && &&&counter.start(); // This restarts timer ok. when when retrying button 1 to cancel again, it never cancels.
// rest of code snipped
// Timer Setup
& & public class MyCount extends CountDownTimer {
& && &&&public MyCount(long millisInFuture, long countDownInterval) {
& && &&&super(millisInFuture, countDownInterval);
& && &&&}& &
& && &&&public void onFinish() {
Intent intent = new Intent();
& && && && &intent.setClass(Main.this, Final.class);
& && && && &startActivity(intent);
& && && && &}
& && && && &else {
& && && && &MyCount counter = new MyCount(5);
& && && && &counter.start();
& && && && &clock = counter.toString();
& && && && &}
& && &&&}& &
& && &&&public void onTick(long millisUntilFinished) {
& && && && &//int min = 5;
& && && && &/*if (+millisUntilFinished / 1000 &= 240) {
& && && && && & min = 4;
& && && && && & mintosec = &240&;
& && && && && & //millisUntilFinished = 59000;
& && && && &}
& && && && &else if (+millisUntilFinished / 1000 &= 180) {
& && && && && & min = 3;
& && && && && & mintosec = &180&; }
& && && && &else if (+millisUntilFinished / 1000 &= 120) {
& && && && && & min = 2;
& && && && && & mintosec = &120&; }
& && && && &else if (+millisUntilFinished / 1000 &= 60)
& && && && && & min =1;
& && && && && & mintosec = &60&; */
& && && && &//TextView totmin = (TextView) findViewById(R.id.clockmin);
& && && && &//totmin.setText(&&);
& && && && &TextView totsec = (TextView) findViewById(R.id.clocksec);
& && && && &totsec.setText(+duration +& : &+ millisUntilFinished / 1000);
& && && && &countcur = millisUntilF // store current count value for pausing and restarting
& & }&&// End Timer
助理工程师
把这句代码前面的MyCount去掉。
MyCount counter= new MyCount(countcur,1000);android利用countdowntimer切换屏幕内容时报错[code=Java][/code]package com.import java.util.ArrayLimport java.util.Rimport android.app.Aimport android.content.Cimport android.content.res.Rimport android.graphics.Cimport android.graphics.Timport android.graphics.drawable.Dimport android.os.Bimport android.os.CountDownTimport android.util.DisplayMimport android.util.Limport android.view.Vimport android.widget.AbsoluteLimport android.widget.TextVpublic class NewActivity extends Activity { &
private MyC &
private TextV&
private NewV &
@Override &
protected void onCreate(Bundle savedInstanceState) { &
// TODO Auto-generated method stub
super.onCreate(savedInstanceState); &
setContentView(R.layout.main);&
Log.i(&line 29&,&1&); &
view = new NewView(null); &
Log.i(&line 31&,&2&); &
//设置背景 &
Resources res = getResources(); &
Drawable drawable = res.getDrawable(R.drawable.bac); &
this.getWindow().setBackgroundDrawable(drawable); &
//设置字体
Typeface typeface = Typeface.createFromAsset(getAssets(), &fonts/LithosPro-Bold.ttf&); &
tv = (TextView)findViewById(R.id.show); &
tv.setTypeface(typeface); &
tv.setTextColor(Color.BLACK); &
//倒计时5s,每次间隔1s &
mc = new MyCount(); &
mc.start(); &
/*定义一个倒计时的内部类*/ &
class MyCount extends CountDownTimer {&
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
//倒计时结束之后做的事 &
public void onFinish() { &
tv.setVisibility(View.INVISIBLE); &
setContentView(view); &
//倒计时的时候 &
public void onTick(long millisUntilFinished) {
tv.setText(&请等待5秒(& + millisUntilFinished / 1000 + &)...&); &
& }/*定义一个显示数字的类*/class NewView extends AbsoluteLayout /*implements OnClickListener*/{ private int count = 5; private TextView[] text = new TextView[count]; private int[] locationx = private int[] locationy = private int[] numbers =
private DisplayM
public NewView(Context context) {
super(context);
// TODO Auto-generated constructor stub
//获取屏幕的尺寸
dm = getResources().getDisplayMetrics(); &
width = dm.widthP &
height = dm.heightP
//设置字体
Typeface typeface = Typeface.createFromAsset(context.getAssets(), &fonts/LithosPro-Bold.ttf&);
numbers = randRange(count,10);
locationx = randRange(count,width-40);
locationy = randRange(count,height-40);
for(int i =0;i&i++){
text[i] = new TextView(context);
addView(text[i],new AbsoluteLayout.LayoutParams(100,100, locationx[i], locationy[i]));

我要回帖

更多关于 countdowntimer 停止 的文章

 

随机推荐