我创建了一个倒计时计时器,它的输出是mm:ss。
因此,如果在此之前切换到activityView,然后使用计时器返回活动,那么一切看起来都很好,但是;-):
所以我知道我必须设置所有这些设置,这是非常正常的。我必须设置
也许你可以让我看看机器人该怎么做?我的工作代码是:
some imports....
public class BasicActivity extends Activity {
ImageButton start, stop;
TextView timer;
/** Called when the activity is first created. */
//Ist dei Verbindung zur XML
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_activity);
//TIMER
start = (ImageButton)findViewById(R.id.startButton);
stop = (ImageButton)findViewById(R.id.stopButton);
timer = (TextView)findViewById(R.id.basicCountDown);
timer.setText(getString(R.string.basic01)); // Platzhalter im Textfeld.
final MyCounter timer = new MyCounter(10000,1000);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
timer.start();
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
}
});
}
public class MyCounter extends CountDownTimer{
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
System.out.println("Timer Completed.");
timer.setText(getString(R.string.basic02));
//Spielt den TON ab, wenn die ZEit um ist. Siehe Methode playSound!
playSound();
//Notification
new AlertDialog.Builder(BasicActivity.this).setTitle(getString(R.string.app_name)).setMessage(getString(R.string.basicNotificationMessage)).setIcon(R.drawable.ic_launcher).setNeutralButton(getString(R.string.ButtonOK), null).show();
}
@Override
public void onTick(long millisUntilFinished) {
SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");
// dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = new Date(millisUntilFinished);
timer.setText(dateFormat.format(date));
// System.out.println("Timer : " + (millisUntilFinished/1000));
System.out.println("Timer : " + dateFormat.format(date));
}
}
//Sound nach Ablauf der Zeit
public void playSound() {
final MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.ton_timer);
mp.start();
}
}发布于 2013-06-07 07:31:31
1.
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setEnabled(false);
timer.start();
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
start.setEnabled(true)
}
});
@Override
public void onFinish() {
System.out.println("Timer Completed.");
timer.setText(getString(R.string.basic02));
start.setEnabled(true);
//Spielt den TON ab, wenn die ZEit um ist. Siehe Methode playSound!
playSound();
//Notification
new AlertDialog.Builder(BasicActivity.this).setTitle(getString(R.string.app_name)).setMessage(getString(R.string.basicNotificationMessage)).setIcon(R.drawable.ic_launcher).setNeutralButton(getString(R.string.ButtonOK), null).show();
}如果用户返回并且计数器仍然是可裁减的,则刷新activityView
这有点棘手。当然,如果您离开该活动,您应该取消CountDownTimer,否则您的活动可能会泄漏。
https://stackoverflow.com/questions/16692129
复制相似问题