首页 文章

CountDownTimer不倒计时但直接跳到finish()方法

提问于
浏览
0

我有一个我正在创建的应用程序,最终将成为具有多个存储次数的倒数计时器,以便用户选择要使用的那个,然后启动倒计时器 . 我遇到的问题是对_469281的调用有效,但定时器跳转到 onfinish() . 我把log.i 's within the override onTick function and those Log.i' s放在logcat中没有出现,这让我相信逻辑中出现了问题,并且倒计时器方法没有看到倒计时的东西并且掉到了完成状态 .

话虽如此,请参阅下面的整个应用程序:

package com.vertygoeclypse.multitimer;
    import android.app.Activity;
    import android.app.Dialog;
    import android.os.Bundle;
    import android.os.CountDownTimer;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.NumberPicker;
    import android.widget.TextView;
    public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener, OnClickListener{
Button dgbtn, abbtn, exbtn, cvbtn, canlbtn, sbtbtn, starest;
EditText tagvalue;
TextView tgview, minview, secview, timeRemaining;
NumberPicker minnp, secnp;
Dialog cusd;
private MultiCountDownTimer countDownTimer;
private long timeElapsed;
private boolean timerHasStarted = false;
private TextView timeElapsedView;
long startTime;
long interval=100;
long coversionvalues=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    dgbtn = (Button) findViewById(R.id.dialogbtn);
    abbtn = (Button) findViewById(R.id.aboutbtn);
    exbtn = (Button) findViewById(R.id.exitbtn);
    cvbtn = (Button) findViewById(R.id.clrvaluesbtn);
    starest =  (Button) findViewById(R.id.startresetbtn);
    tgview = (TextView) findViewById(R.id.tagview);
    minview = (TextView) findViewById(R.id.minview);
    secview = (TextView) findViewById(R.id.seecview);
    timeRemaining = (TextView) findViewById(R.id.timeremainingview);
    timeElapsedView = (TextView) findViewById(R.id.countdowntimer);
    dgbtn.setOnClickListener(this);
    abbtn.setOnClickListener(this);
    exbtn.setOnClickListener(this);
    cvbtn.setOnClickListener(this);
    starest.setOnClickListener(this);
    countDownTimer = new MultiCountDownTimer(startTime, interval);
    if(startTime==0){
        starest.setEnabled(false);
    }else if(startTime>0){
        starest.setEnabled(true);
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
    // TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.dialogbtn:
        showDialog();
        break;
    case R.id.exitbtn:
        finish();
    case R.id.clrvaluesbtn:
        tgview.setText("");
        minview.setText("");
        secview.setText("");
        break;
    case R.id.cancelbtn:
        cusd.dismiss();
    case R.id.submitbtn:
        tgview.setText(String.valueOf(tagvalue.getText()));
        minview.setText(String.valueOf(minnp.getValue()));
        secview.setText(String.valueOf(secnp.getValue()));
        int val1 = Integer.parseInt(String.valueOf(minnp.getValue()));
        int val2 = Integer.parseInt(String.valueOf(secnp.getValue()));
        int val3 = (val1*60)*1000;
        int val4 = val2*1000;
        coversionvalues = Long.valueOf(String.valueOf(val3+val4));
        startTime = coversionvalues;
        starest.setEnabled(true);
        cusd.dismiss(); 
        break;
    case R.id.startresetbtn:
        if(!timerHasStarted){
                countDownTimer.start();
                timerHasStarted = true;
                starest.setText("Start");
        } else {
            countDownTimer.cancel();
            timerHasStarted = false;
            starest.setText("Reset");
        }
    }
}
public void showDialog(){
    cusd = new Dialog(MainActivity.this);
    cusd.setTitle("Tag and Timer Selector");
    cusd.setContentView(R.layout.dialogbox);
    canlbtn = (Button) cusd.findViewById(R.id.cancelbtn);
    sbtbtn = (Button) cusd.findViewById(R.id.submitbtn);        
    minnp = (NumberPicker) cusd.findViewById(R.id.numberPicker1);
    secnp = (NumberPicker) cusd.findViewById(R.id.numberPicker2);
    tagvalue = (EditText) cusd.findViewById(R.id.tagname);
    canlbtn.setOnClickListener(this);
    sbtbtn.setOnClickListener(this);
    minnp.setMaxValue(59);
    minnp.setMinValue(0);
    minnp.setWrapSelectorWheel(false);
    minnp.setOnValueChangedListener(this);
    secnp.setMaxValue(59);
    secnp.setMinValue(0);
    secnp.setWrapSelectorWheel(false);
    secnp.setOnValueChangedListener(this);
    cusd.show();
}
public class MultiCountDownTimer extends CountDownTimer{
    public MultiCountDownTimer(long startTime, long interval)
    {
        super(startTime, interval);
    }
@Override
public void onTick(long millisUntilFinished)
    {
        Log.i("VertygoEclypse", String.valueOf(millisUntilFinished));
        timeRemaining.setText("Time remain:" + millisUntilFinished);
        timeElapsed = startTime - millisUntilFinished;
        Log.i("VertygoEclypse", String.valueOf(timeElapsed));
        timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
    }
@Override
public void onFinish()
    {
        timeRemaining.setText("Time's up!");
        timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
    }
}
    }

1 回答

  • 1

    对于startTime设置一些值,它不能为零 . 实际上startTime是倒数计时器应该停止或完成的未来时间 . 如果可能请将其更改为完成时间

    countDownTimer = new MultiCountDownTimer(startTime, interval);
    

相关问题