我有这样的课:
public class TimerActivity extends Activity
{
CountDownTimer cntTimer = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timers);
final ImageButton startTimerButton = (ImageButton)findViewById(R.id.timer1ImageButton);
final EditText timerText = (EditText) findViewById(R.id.timerEditText1);
final TextView timerTextValue = (TextView) findViewById(R.id.timerTextView);
boolean checkstate =false;
timerCountDown(checkstate,startTimer3Button,timerText3, timerTextValue3);
}
public void timerCountDown(boolean check,final ImageButton startTimerImageButton ,
final EditText timerText,final TextView timerTextValue)
{
Integer input = 0;
if(timerText.getText().toString()!="")
{
input = Integer.parseInt(timerText.getText().toString())*1000 ;
}
CountDownTimer timer = new CountDownTimer(input, 1000)
{
public void onTick(long millisUntilFinished)
{
timerTextValue.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish()
{
timerTextValue.setText("done!");
}
};
timerStatus(check,startTimerImageButton,timer);
}
public void timerStatus(final boolean checkstate, final ImageButton startTimer3Button ,final CountDownTimer downTimer)
{
startTimer3Button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if(checkstate==false)
{
startTimer3Button.setImageResource(R.drawable.reset);
//Error
checkstate = true;
downTimer.start();
}
else
{
startTimer3Button.setImageResource(R.drawable.start);
//Error
checkstate = false;
downTimer.cancel();
}
}
});
}
}
但是我在timerStatue方法上收到以下错误:checkstate = false和checkstate = true:
最终局部变量checkstate不能被赋值,因为它是用封闭类型定义的!
我搜索了google和stackoverflow,但是找不到与我的问题兼容的答案!
你能帮助我吗?
提前致谢!
最佳答案
我不知道您是否打算使用您的代码。有点模糊。正如Yazan所说,最好实现OnClickListener
接口,以避免将final发送给它的方法。
之后,为什么一切都是final
?您必须在类级别定义变量(类变量)才能从其他方法轻松访问它们,并且onCreate()
方法设置其主要值。
因此,无论我如何像下面那样固定您的代码,都没有任何错误,但是我仍然没有做到这一点:
public class TimerActivity extends Activity
{
CountDownTimer cntTimer = null;
ImageButton startTimerButton;
EditText timerText;
TextView timerTextValue;
boolean checkstate =false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timers);
startTimerButton = (ImageButton)findViewById(R.id.timer1ImageButton);
timerText = (EditText) findViewById(R.id.timerEditText1);
timerTextValue = (TextView) findViewById(R.id.timerTextView);
timerCountDown();
}
public void timerCountDown()
{
Integer input = 0;
if(timerText.getText().toString()!="")
{
input = Integer.parseInt(timerText.getText().toString())*1000 ;
}
CountDownTimer timer = new CountDownTimer(input, 1000)
{
public void onTick(long millisUntilFinished)
{
timerTextValue.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish()
{
timerTextValue.setText("done!");
}
};
timerStatus(timer);
}
public void timerStatus(final CountDownTimer downTimer)
{
startTimerButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if(checkstate==false)
{
startTimerButton.setImageResource(android.R.drawable.ic_secure);
//Error
checkstate = true;
downTimer.start();
}
else
{
startTimerButton.setImageResource(android.R.drawable.ic_delete);
//Error
checkstate = false;
downTimer.cancel();
}
}
});
}
}