在我的应用程序中,我保留了暂停按钮和开始按钮。 默认情况下,我将计时器设置为50秒。当计时器运行时,我为每个按钮编写了onclick动作代码...即,单击暂停按钮时,暂停时间,再次点击开始按钮时它再次从50秒开始。 但它应该从暂停的时间开始。我该怎么做?
请帮助我......提前致谢.......
我的代码:
long timervalue = 50000;
btn13 = (ImageView) findViewById(R.id.pause);
btn14 = (ImageView) findViewById(R.id.resume);
final CountDownTimer Counter1 = new CountDownTimer(timervalue, 1000) {
public void onTick(long millisUntilFinished) {
time.setText(formatTime(millisUntilFinished));
btn13.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Counter1.cancel();
}
});
btn14.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Counter1.start();
}
});
}
public void onFinish()
{
Counter1.cancel();
}
};
public String formatTime(long millis) {
String output = "00:00";
long seconds = millis / 1000;
// long minutes = seconds / 60;
seconds = seconds % 60;
// minutes = minutes % 60;
String secondsD = String.valueOf(seconds);
// String minutesD = String.valueOf(minutes);
if (seconds < 10)
secondsD = "0" + seconds;
// if (minutes < 10)
// minutesD = "0" + minutes;
output = secondsD;
return output;
}
答案 0 :(得分:0)
不幸的是,你不能暂停CountDownTimer,但还有其他方法可以做你想做的事。
试试这个:
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
private CountDownTimer ct;
private long ts = 30;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView mTextField = (TextView) this.findViewById(R.id.txt);
final Button bt = (Button) this.findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(bt.getText().toString().equals("Start")){
bt.setText("Stop");
ct = new CountDownTimer(ts*1000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
ts = millisUntilFinished/1000;
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
}
else{
bt.setText("Start");
ct.cancel();}
}
});
}
}