그래서 저는 스톱워치와 같은 타이머를 만들려고 노력하고 있지만 완전한 멍청한 행동입니다. 나는 Here과 Here에서 "결합"하려고했습니다.사용자 입력 및 타이머 (java android app)
목표는 타이머를 설정하려는 기간에 대한 사용자 입력을받는 것입니다. 그러면 시간이 다되었을 때 물건이됩니다. 주의 할
package com.example.timer;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
public TextView text;
private final long interval = 1 * 1000;
EditText editTime1;
Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTime1 = (EditText)findViewById(R.id.editTime1);
startButton = (Button)findViewById(R.id.startButton);
text = (TextView) this.findViewById(R.id.timer);
startButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//get the name from edittext and storing into string variable
long timeVal = Long.parseLong(editTime1.getText().toString());
countDownTimer = new MyCountDownTimer(timeVal, interval);
text.setText(text.getText() + String.valueOf(timeVal/1000));
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startButton.setText("STOP");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startButton.setText("RESTART");
}
}
class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long timeVal, long interval) {
super(timeVal, interval);
}
@Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished/1000);
}
@Override
public void onFinish() {
text.setText("Times up");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
이 코드의 문제점은 무엇입니까? – SoulRayder
카운트 다운되지 않습니다. 버튼이 멈추도록 바뀌지 만 카운트 다운 대신 "타임업"이 표시됩니다. 버튼을 다시 누르면 다시 시작되고 "Times up0"이 표시됩니다. – cflinspach