我正在尝试制作一个倒数计时器,但我不知道如何放置一个按钮来开始/停止倒数。我试图在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));
}
}
} else {
if (segundos>9){
timeView.setText("00:" + String.valueOf(segundos));
} else {
timeView.setText("00:0" + String.valueOf(segundos));
}
}
}
@Override
public void onFinish() {
timeView.setText("Time out!");
mplayer.start();
}
}.start();
}
}
这是我目前所拥有的。非常感谢您的帮助。
答案 0 :(得分:0)
在a = Armies[army];
u = a.Units[unit]
actions[attribute](a, u);
文件中,将此行添加到您的按钮
XML
并将此方法添加到您的活动中
android:onClick="startCounterDown"