将通用变量作为long分配给CountDownTimer

时间:2014-01-23 00:09:45

标签: java android countdowntimer

下午好,

我希望分配一个通用变量,可以在整个应用程序中进行更改并传递给CountDownTimer作为我的长

package com.rcd.learningactivities;

import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;

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

public class MainActivity extends Activity {

    protected CountDownTimer cd;
    private long countTime = 0;  // Variable for CountDownTimer
    private Button lastPressedButton;
    private Button red;
    private Button blue;
    private TextView blueTimer;
    private TextView redTimer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        blueTimer = (TextView) findViewById(R.id.blueTimer); 
        blue = (Button) findViewById(R.id.button1);

        redTimer = (TextView) findViewById(R.id.redTimer); 
        red = (Button) findViewById(R.id.button2);


        cd = new CountDownTimer(countTime, 1000) {  //<--- trying to use countTime here

             public void onTick(long millisUntilFinished) {
                    DecimalFormat dfmin = new DecimalFormat("0");
                    DecimalFormat dfsec = new DecimalFormat("00");
                    double seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)%60;
                    double min = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
                    if (lastPressedButton == blue){
                        blueTimer.setText(String.valueOf(dfmin.format(min)) + ":" + String.valueOf(dfsec.format(seconds)));
                    }
                    else if (lastPressedButton == red) {
                        redTimer.setText(String.valueOf(dfmin.format(min)) + ":" + String.valueOf(dfsec.format(seconds)));
                    }

             }

             public void onFinish() {
                    if (lastPressedButton == blue){
                        blueTimer.setText("5:00");
                    }
                    else if (lastPressedButton == red){
                        redTimer.setText("5:00");
                    }
                }
          };

        blue.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                lastPressedButton = blue;
                countTime = 300000; // <-- setting countTime here
                if (blueTimer.getText().toString().contains("5:00")){
                    cd.start();

                } else {
                    cd.cancel();
                    cd.onFinish();
                }

            }
        });

        red.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                lastPressedButton = red;
                countTime = 250000;
                if (redTimer.getText().toString().contains("5:00")){
                    cd.start();
                } else {
                    cd.cancel();
                    cd.onFinish();
                }

            }
        });
    }

    @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;
    }

}

无论出于何种原因,它保持最初声明的数字0(或者如果我将其更改为300000然后它总是300000)并且它不会在我的onClick中更改下面的countTime。

非常感谢任何帮助。如果可以附上解释,那就太棒了!

提前感谢大家!

编辑:我猜它是一个范围问题,我忽略了吗?

EDIT2:如果这是一个范围问题,我有点困惑的是如何能够重置“lastPressedButton”变量设置它,而不是countTime。

1 个答案:

答案 0 :(得分:1)

Java已经通过值语义传递,而不是通过引用传递,构造中的countTime值传递给CountDownTimer,CDT在实例化后没有看到对countTime的任何更新。