2

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

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;

        }

    }
}

请帮忙 ...........

4

1 回答 1

1

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

于 2012-05-27T11:18:11.560 回答