这是我的活动的简化版本。我有一个计时器,每秒都会调用onChronometerTick
。在方法中声明和初始化局部变量会不会有效?
或者更好的做法是在方法之外声明变量,只是在onChronometerTick
内部更改它们的值
public class MainActivity extends Activity implements Chronometer.OnChronometerTickListener {
//Declaring View Variables
private Chronometer timer; //The timer
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialising View Variables
timer = ((Chronometer)findViewById(R.id.chronometer));
timer.setOnChronometerTickListener(this);
}
public void onChronometerTick(Chronometer chronometer) {
//Local variables
String timerTime = (chronometer.getText()).toString(); //Gets the string value of the timer
Boolean stop = false;
Boolean halfTime = false;
Boolean fullTime = false;
switch (timerTime){
case "00:10":
stop = true;
break;
case "00:20":
halfTime = true;
break;
case "00:30":
stop = true;
break;
case "00:40":
stop = true;
fullTime = true;
break;
}
}
}