如何从倒计时器减去一些秒?

时间:2012-05-27 11:08:38

标签: java android

我正在为一个测验应用程序尝试一个简单的计数器。我想在按下按钮时减去秒钟的一些秒数。我使用了包含真值的标志..当按下按钮时它变为假..然后我检查标志的值,并尝试从计时器减去几秒钟......但它不起作用。 这是我试过的代码......

java代码:

package com.okj.hjhu;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class OkghActivity extends Activity implements OnClickListener {

    TextView tv;
    Button a;
    Boolean flag = true;
    CountDownTimer counter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Initializer();
        setlisteners();

        MyCount counter = new MyCount(120000, 1000);

        counter.start();

    }

    private void setlisteners() {
        // TODO Auto-generated method stub

        a.setOnClickListener(this);

    }

    private void Initializer() {
        tv = (TextView) findViewById(R.id.textView1);

        a = (Button) findViewById(R.id.button1);

    }

    public class MyCount extends CountDownTimer {

        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

        }

        @Override
        public void onFinish() {
            tv.setText("done");
        }

        @Override
        public void onTick(long millisUntilFinished) {

            tv.setText("Lef Time ...  00:00:" + millisUntilFinished / 1000);

            if (flag = false) {

                long ty = millisUntilFinished - 9000;

                tv.setText("Lef Time ...  00:00:" + ty / 1000);

            }

        }

    }

    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {

        case R.id.button1:
//when the button will be pressed the counter will minus some second from the timer
            flag = false;
            break;

        case R.id.button2:
            break;

        case R.id.button3:
            break;

        case R.id.button4:
            break;

        }

    }
}

请帮助...........

1 个答案:

答案 0 :(得分:1)

我认为方法是,您必须使用Counterdowntimer.cancel()取消当前的倒计时器,然后在新的特定时间启动新的计时器/