Android暂停计时器

时间:2014-05-28 16:59:16

标签: android timer

嗨,这是我的代码:

public class Match extends Activity{
    TextView timervalue;
    long timeInMilliseconds = 0L;
    long timeSwapBuff = 0L;
    long updatedTime = 0L;
    String secondHalf;
    private long startTime = 0L;
    private Handler customHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.match);

        secondHalf = null;
        Button startButton;
        Button l;
        timervalue = (TextView) findViewById(R.id.timerValue);
        startButton = (Button) findViewById(R.id.startButton);
        l = (Button) findViewById(R.id.button1);
        l.setOnClickListener(new View.OnClickListener() {



            public void onClick(View view) {

                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(firstHalf);
            }

        });
        startButton.setOnClickListener(new View.OnClickListener() {



            public void onClick(View view) {

                startTime = SystemClock.uptimeMillis();
                customHandler.postDelayed(firstHalf, 0);
            }

        });

}


    private Runnable firstHalf = new Runnable() {
        public void run() {
            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (updatedTime / 1000);
        //    int mins = secs / 60;

            secs = secs % 91;

            int milliseconds = (int) (updatedTime % 1000);

       //    timervalue.setText("" + mins + ":"
            timervalue.setText("" + "minuto" + ":"
                    + String.format("%02d", secs)); //+ ":"

                 //   + String.format("%03d", milliseconds));

            customHandler.postDelayed(this, 0);
            if ( secs == 10 ) {
                secondHalf = "X";
                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(firstHalf);  
            }
        }





    };



}

为什么我能够停止计时器并恢复点击按钮,等待10秒(如代码中)并单击按钮后无法恢复计时器?有什么变化?

这不起作用:计时器停止但没有恢复原因

             if ( secs == 10 ) {
            secondHalf = "X";
            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(firstHalf);  
        }

0 个答案:

没有答案