我已经通过链接 http://dewful.com/?tag=basic-不见了Android的计时器关于在Android上的定时器应用程序。这是工作的罚款。 我需要从我的停止再次添加暂停按钮来停止定时器和一个播放按钮开始计时。我能做到这一点的任务?
我的code:
长timervalue = 50000;
CountDownTimer计数器1 =新CountDownTimer(timervalue,1000)
{
公共无效onTick(最终长millisUntilFinished)
{
time.setText(formatTime(millisUntilFinished));
pause.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图v)
{
Counter1.cancel();
}
}
);
resume.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图v)
{
Counter1.start();
timervalue =的Long.parseLong(输出);
的System.out.println(暂停定时器值恢复+ timervalue);
Counter1.onTick(timervalue);
}
}
);
}
公共无效onFinish()
{
Counter1.cancel();
}
};
公共字符串formatTime(long millis)来
{
输出=00;
秒=米利斯/ 1000;
秒=秒%60;
的System.out.println(秒在这里+秒);
字符串secondsD =将String.valueOf(秒);
的System.out.println(secondsD这里+ secondsD);
如果(秒和放大器; LT;
10)secondsD =0+秒;
的System.out.println(secondsD在这里,如果+ secondsD);
输出= secondsD;
返回输出;
}
在当点击计时器从50秒重新开始,我不希望这样的恢复按钮上面的code。它应该从哪里我停下的时候开始。请帮我关于这个...我因为1周争取这个......
将非常感谢您的帮助..........
解决方案 公共类TimerActivity扩展活动
{
E1的EditText;
mycount的计数器;
龙S1;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
E1 =(EditText上)findViewById(R.id.editText1);
计数器=新mycount的(5000,1000);
counter.start();
}
公共无效航空自卫队(视图v)
{
开关(v.getId())
{
案R.id.button1:counter.cancel();
打破;
案例R.id.button2:反=新mycount的(s1,1000);
counter.start();
}
}
公共类mycount的扩展CountDownTimer
{
公共mycount的(长millisInFuture,长countDownInterval)
{
超(millisInFuture,countDownInterval);
}
@覆盖公共无效onFinish()
{
e1.setText(DONE);
}
@覆盖公共无效onTick(长millisUntilFinished)
{
S1 = millisUntilFinished;
e1.setText(左+ millisUntilFinished / 1000);
}
}
}
I have gone through the link http://dewful.com/?tag=basic-android-timer regarding the timer application in android. It is working fine. I need to add the pause button to stop the timer and a play button to start the timer again from where I stopped. Can I achieve that task?
My Code:
long timervalue = 50000;
CountDownTimer Counter1 = new CountDownTimer(timervalue, 1000)
{
public void onTick(final long millisUntilFinished)
{
time.setText(formatTime(millisUntilFinished));
pause.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Counter1.cancel();
}
}
);
resume.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Counter1.start();
timervalue = Long.parseLong(output);
System.out.println("paused timer value resumed"+timervalue);
Counter1.onTick(timervalue);
}
}
);
}
public void onFinish()
{
Counter1.cancel();
}
};
public String formatTime(long millis)
{
output = "00";
seconds = millis / 1000;
seconds = seconds % 60;
System.out.println("seconds here"+seconds);
String secondsD = String.valueOf(seconds);
System.out.println("secondsD here"+secondsD);
if (seconds <
10) secondsD = "0" + seconds;
System.out.println("secondsD here in if"+secondsD);
output = secondsD;
return output;
}
In the above code when resume button is clicked the timer is again starting from 50sec and I don't want like that. It should start from the time where I paused. Please help me regarding this...I am struggling for this since one week......
Will be really thankful for the help..........
解决方案public class TimerActivity extends Activity
{
EditText e1;
MyCount counter;
Long s1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1=(EditText)findViewById(R.id.editText1);
counter= new MyCount(5000,1000);
counter.start();
}
public void asdf(View v)
{
switch(v.getId())
{
case R.id.button1: counter.cancel();
break;
case R.id.button2: counter= new MyCount(s1,1000);
counter.start();
}
}
public class MyCount extends CountDownTimer
{
public MyCount(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
@Override public void onFinish()
{
e1.setText("DONE");
}
@Override public void onTick(long millisUntilFinished)
{
s1=millisUntilFinished;
e1.setText("left:"+millisUntilFinished/1000);
}
}
}
相关推荐
最新文章