我正在尝试制作一个倒数计时器,但我无法实现如何放置按钮来开始/停止倒计时。我试图在 onCreate 方法中创建该方法,但它返回一个错误,所以我的脚本是这样的(其中 goButton 是我的 onClick 函数):
package com.example.ricardofonzo.eggtimer;
import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
MediaPlayer mplayer;
int estadoContador =0;
public void goButton(View view){
estadoContador = 1;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mplayer = MediaPlayer.create(this,R.raw.bell);
final TextView timeView = (TextView) findViewById(R.id.timeView);
CountDownTimer cuentaRegresiva = new CountDownTimer(100000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
long segundos = millisUntilFinished/1000;
long minutos;
if (segundos>= 60) {
minutos = segundos / 60;
segundos = segundos - minutos * 60;
if (minutos > 9) {
if (segundos > 9) {
timeView.setText(String.valueOf(minutos) + ":" + String.valueOf(segundos));
} else {
timeView.setText(String.valueOf(minutos) + ":0" + String.valueOf(segundos));
}
} else {
if (segundos > 9) {
timeView.setText("0" + String.valueOf(minutos) + ":" + String.valueOf(segundos));
} else {
timeView.setText("0" + String.valueOf(minutos) + ":0" + String.valueOf(segundos));
}
}
}
这就是我目前所拥有的。我将非常感谢您的帮助。
慕码人2483693
相关分类