倒数计时器问题
我正在使用倒计时器并对其进行设置,以便用户可以对其进行设置。当计时器到达 00:00 时,它应该说“完成!”。出于某种原因,我正在“完成!”就在我启动计时器之后。点击暂停和恢复后,它会显示我的计数器正在倒计时,但我不太确定客户是否会对此感到满意。谁能告诉我为什么会遇到这个问题?谢谢。
package score.keeper.basketball;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class Score extends Activity implements AdapterView.OnItemSelectedListener {
private Button Button01;
private Button Button02;
private Button Button03;
private Button Button04;
private TextView TextView01;
private TextView TextView02;
private TextView TextView03;
private TextView TextView06;
private Spinner Spinner01;
private Spinner Spinner02;
private TextView Team01;
private TextView Team02;
TextView timeDisplay;
int state = 0;
int length;
long startTime = 0;
long currentTime = 0;
long timeElapsed = 0;
long timeRemaining = 0;
long prevTimeRemaining = 0;
Button control;
MyCount counter;
String[] goal={"Three", "Foul", "Two"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button01 = (Button) findViewById(R.id.Button01);
Button02 = (Button) findViewById(R.id.Button02);
Button03 = (Button) findViewById(R.id.Button03);
Button04 = (Button) findViewById(R.id.Button04);
TextView01 = (TextView) findViewById(R.id.TextView01);
TextView02 = (TextView) findViewById(R.id.TextView02);
TextView03 = (TextView) findViewById(R.id.TextView03);
TextView06 = (TextView) findViewById(R.id.TextView06);
Team01 = (TextView) findViewById(R.id.TextView04);
Team02 = (TextView) findViewById(R.id.TextView05);
Team01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(Score.this);
dialog.setContentView(R.layout.setteamname);
dialog.setTitle("Team Name");
dialog.setCancelable(true);
final EditText teamName = (EditText) dialog.findViewById(R.id.teamName);
Button btn = (Button) dialog.findViewById(R.id.setText);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String teamName2 = teamName.getText().toString();
Team01.setText("" + teamName2);
dialog.dismiss();
}
});
dialog.show();
}});
Team02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(Score.this);
dialog.setContentView(R.layout.setteamname);
dialog.setTitle("Team Name");
dialog.setCancelable(true);
final EditText teamName = (EditText) dialog.findViewById(R.id.teamName);
Button btn = (Button) dialog.findViewById(R.id.setText);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String teamName3 = teamName.getText().toString();
Team02.setText("" + teamName3);
dialog.dismiss();
}
});
dialog.show();
}});
Spinner spin = (Spinner) findViewById(R.id.Spinner01);
spin.setOnItemSelectedListener((OnItemSelectedListener) this);
ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, goal);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
Button01.setOnLongClickListener(subtractLeft);
Button02.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){my_func1();}});
Button03.setOnLongClickListener(subtractRight);
Button04.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){my_func2();}});
timeDisplay = (TextView) findViewById(R.id.timer);
timeDisplay.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final Dialog dialog = new Dialog(Score.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Set Time");
dialog.setCancelable(true);
final EditText min = (EditText) dialog.findViewById(R.id.min);
final EditText sec = (EditText) dialog.findViewById(R.id.sec);
Button btn = (Button)dialog.findViewById(R.id.positive);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
int min2 = Integer.parseInt(min.getText().toString());
int sec2 = Integer.parseInt(sec.getText().toString());
{length = (((min2 * 60) + sec2) * 1000);
dialog.dismiss();}
}});
dialog.show();}
});
counter = new MyCount(length, 100);
control = (Button) findViewById(R.id.control);
}
public void control(View view) {
switch (state) {
case 0:
startTime = System.currentTimeMillis();
counter.start();
control.setText(R.string.pause);
state = 1;
break;
case 1:
// pause
currentTime = System.currentTimeMillis();
timeElapsed = currentTime - startTime;
if (prevTimeRemaining == 0)
timeRemaining = length - timeElapsed;
else
timeRemaining = prevTimeRemaining - timeElapsed;
counter.cancel();
timeDisplay.setText(formatTime(timeRemaining));
control.setText(R.string.resume);
prevTimeRemaining = timeRemaining;
// resume
counter = new MyCount(timeRemaining, 100);
state = 0;
break;
case 2:
prevTimeRemaining = 0;
counter = new MyCount(length, 100);
control.setText(R.string.start);
timeDisplay.setText(R.string.timer);
state = 0;
}
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
timeDisplay.setText("done!");
state = 2;
control.setText(R.string.restart);
}
public void onTick(long millisUntilFinished) {
timeDisplay.setText(formatTime(millisUntilFinished));
}
}
public String formatTime(long millis) {
String output = "00:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
// long hours = minutes / 60;
seconds = seconds % 60;
minutes = minutes % 60;
// hours = hours % 60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
// String hoursD = String.valueOf(hours);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
// if (hours < 10)
// hoursD = "0" + hours;
output = minutesD + " : " + secondsD;
return output;
}
public void setSpinner02(Spinner spinner02) {
Spinner02 = spinner02;
}
public Spinner getSpinner02() {
return Spinner02;
}
public void setSpinner01(Spinner spinner01) {
Spinner01 = spinner01;
}
public Spinner getSpinner01() {
return Spinner01;
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
{
if ("Three".equals(goal[position]))
{TextView03.setText("3");}
if ("Foul".equals(goal[position]))
{TextView03.setText("1");}
if ("Two".equals(goal[position]))
{TextView03.setText("2");}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
TextView03.setText("2");
TextView06.setText("2");
}
void my_func1() {
int scoreLeft;
int addOne;
int scoreLeftTotal;
addOne = Integer.parseInt(TextView03.getText().toString());
scoreLeft = Integer.parseInt(TextView01.getText().toString());
scoreLeftTotal = (scoreLeft + addOne);
TextView01.setText("" + scoreLeftTotal);
}
void my_func2() {
int scoreRight = Integer.parseInt(TextView02.getText().toString());
int addOne = Integer.parseInt(TextView03.getText().toString());
{int scoreRightTotal = (scoreRight + addOne);
TextView02.setText("" + scoreRightTotal);
}
}
private OnLongClickListener subtractLeft = new OnLongClickListener() {
public boolean onLongClick(View v) {
int scoreLeft = Integer.parseInt(TextView01.getText().toString());
int addOne = Integer.parseInt(TextView03.getText().toString());
{int scoreLeftTotal = (scoreLeft - addOne);
TextView01.setText("" + scoreLeftTotal);
}
return true;
}
};
private OnLongClickListener subtractRight = new OnLongClickListener() {
public boolean onLongClick(View v) {
int scoreRight = Integer.parseInt(TextView02.getText().toString());
int addOne = Integer.parseInt(TextView03.getText().toString());
{int scoreLeftTotal = (scoreRight - addOne);
TextView02.setText("" + scoreLeftTotal);
}
return true;
}
};
public void OnClick(View view) {
if (view==Team01){
Dialog dialog = new Dialog(Score.this);
dialog.setContentView(R.layout.setteamname);
dialog.setTitle("Team Name");
dialog.setCancelable(true);
final EditText teamName = (EditText) dialog.findViewById(R.id.teamName);
teamName.setText(R.id.TextView04);
Button btn = (Button)dialog.findViewById(R.id.setText);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
teamName.getText().toString();
Team01.setText("" + teamName);
}
});
dialog.show();}
};
}
I am working with a countdown timer and have it set so that it is able to be set by the user. When the timer gets to 00:00 its supposed to say "done!". For some reason I'm getting the "done!" right after I start the timer. After hitting pause and resume it shows my counter counting down but I'm not so sure that customers are gonna be cool with this. Can anyone tell why I'm having this problem? Thanks.
package score.keeper.basketball;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class Score extends Activity implements AdapterView.OnItemSelectedListener {
private Button Button01;
private Button Button02;
private Button Button03;
private Button Button04;
private TextView TextView01;
private TextView TextView02;
private TextView TextView03;
private TextView TextView06;
private Spinner Spinner01;
private Spinner Spinner02;
private TextView Team01;
private TextView Team02;
TextView timeDisplay;
int state = 0;
int length;
long startTime = 0;
long currentTime = 0;
long timeElapsed = 0;
long timeRemaining = 0;
long prevTimeRemaining = 0;
Button control;
MyCount counter;
String[] goal={"Three", "Foul", "Two"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button01 = (Button) findViewById(R.id.Button01);
Button02 = (Button) findViewById(R.id.Button02);
Button03 = (Button) findViewById(R.id.Button03);
Button04 = (Button) findViewById(R.id.Button04);
TextView01 = (TextView) findViewById(R.id.TextView01);
TextView02 = (TextView) findViewById(R.id.TextView02);
TextView03 = (TextView) findViewById(R.id.TextView03);
TextView06 = (TextView) findViewById(R.id.TextView06);
Team01 = (TextView) findViewById(R.id.TextView04);
Team02 = (TextView) findViewById(R.id.TextView05);
Team01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(Score.this);
dialog.setContentView(R.layout.setteamname);
dialog.setTitle("Team Name");
dialog.setCancelable(true);
final EditText teamName = (EditText) dialog.findViewById(R.id.teamName);
Button btn = (Button) dialog.findViewById(R.id.setText);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String teamName2 = teamName.getText().toString();
Team01.setText("" + teamName2);
dialog.dismiss();
}
});
dialog.show();
}});
Team02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(Score.this);
dialog.setContentView(R.layout.setteamname);
dialog.setTitle("Team Name");
dialog.setCancelable(true);
final EditText teamName = (EditText) dialog.findViewById(R.id.teamName);
Button btn = (Button) dialog.findViewById(R.id.setText);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String teamName3 = teamName.getText().toString();
Team02.setText("" + teamName3);
dialog.dismiss();
}
});
dialog.show();
}});
Spinner spin = (Spinner) findViewById(R.id.Spinner01);
spin.setOnItemSelectedListener((OnItemSelectedListener) this);
ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, goal);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
Button01.setOnLongClickListener(subtractLeft);
Button02.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){my_func1();}});
Button03.setOnLongClickListener(subtractRight);
Button04.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){my_func2();}});
timeDisplay = (TextView) findViewById(R.id.timer);
timeDisplay.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final Dialog dialog = new Dialog(Score.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Set Time");
dialog.setCancelable(true);
final EditText min = (EditText) dialog.findViewById(R.id.min);
final EditText sec = (EditText) dialog.findViewById(R.id.sec);
Button btn = (Button)dialog.findViewById(R.id.positive);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
int min2 = Integer.parseInt(min.getText().toString());
int sec2 = Integer.parseInt(sec.getText().toString());
{length = (((min2 * 60) + sec2) * 1000);
dialog.dismiss();}
}});
dialog.show();}
});
counter = new MyCount(length, 100);
control = (Button) findViewById(R.id.control);
}
public void control(View view) {
switch (state) {
case 0:
startTime = System.currentTimeMillis();
counter.start();
control.setText(R.string.pause);
state = 1;
break;
case 1:
// pause
currentTime = System.currentTimeMillis();
timeElapsed = currentTime - startTime;
if (prevTimeRemaining == 0)
timeRemaining = length - timeElapsed;
else
timeRemaining = prevTimeRemaining - timeElapsed;
counter.cancel();
timeDisplay.setText(formatTime(timeRemaining));
control.setText(R.string.resume);
prevTimeRemaining = timeRemaining;
// resume
counter = new MyCount(timeRemaining, 100);
state = 0;
break;
case 2:
prevTimeRemaining = 0;
counter = new MyCount(length, 100);
control.setText(R.string.start);
timeDisplay.setText(R.string.timer);
state = 0;
}
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
timeDisplay.setText("done!");
state = 2;
control.setText(R.string.restart);
}
public void onTick(long millisUntilFinished) {
timeDisplay.setText(formatTime(millisUntilFinished));
}
}
public String formatTime(long millis) {
String output = "00:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
// long hours = minutes / 60;
seconds = seconds % 60;
minutes = minutes % 60;
// hours = hours % 60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
// String hoursD = String.valueOf(hours);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
// if (hours < 10)
// hoursD = "0" + hours;
output = minutesD + " : " + secondsD;
return output;
}
public void setSpinner02(Spinner spinner02) {
Spinner02 = spinner02;
}
public Spinner getSpinner02() {
return Spinner02;
}
public void setSpinner01(Spinner spinner01) {
Spinner01 = spinner01;
}
public Spinner getSpinner01() {
return Spinner01;
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
{
if ("Three".equals(goal[position]))
{TextView03.setText("3");}
if ("Foul".equals(goal[position]))
{TextView03.setText("1");}
if ("Two".equals(goal[position]))
{TextView03.setText("2");}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
TextView03.setText("2");
TextView06.setText("2");
}
void my_func1() {
int scoreLeft;
int addOne;
int scoreLeftTotal;
addOne = Integer.parseInt(TextView03.getText().toString());
scoreLeft = Integer.parseInt(TextView01.getText().toString());
scoreLeftTotal = (scoreLeft + addOne);
TextView01.setText("" + scoreLeftTotal);
}
void my_func2() {
int scoreRight = Integer.parseInt(TextView02.getText().toString());
int addOne = Integer.parseInt(TextView03.getText().toString());
{int scoreRightTotal = (scoreRight + addOne);
TextView02.setText("" + scoreRightTotal);
}
}
private OnLongClickListener subtractLeft = new OnLongClickListener() {
public boolean onLongClick(View v) {
int scoreLeft = Integer.parseInt(TextView01.getText().toString());
int addOne = Integer.parseInt(TextView03.getText().toString());
{int scoreLeftTotal = (scoreLeft - addOne);
TextView01.setText("" + scoreLeftTotal);
}
return true;
}
};
private OnLongClickListener subtractRight = new OnLongClickListener() {
public boolean onLongClick(View v) {
int scoreRight = Integer.parseInt(TextView02.getText().toString());
int addOne = Integer.parseInt(TextView03.getText().toString());
{int scoreLeftTotal = (scoreRight - addOne);
TextView02.setText("" + scoreLeftTotal);
}
return true;
}
};
public void OnClick(View view) {
if (view==Team01){
Dialog dialog = new Dialog(Score.this);
dialog.setContentView(R.layout.setteamname);
dialog.setTitle("Team Name");
dialog.setCancelable(true);
final EditText teamName = (EditText) dialog.findViewById(R.id.teamName);
teamName.setText(R.id.TextView04);
Button btn = (Button)dialog.findViewById(R.id.setText);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
teamName.getText().toString();
Team01.setText("" + teamName);
}
});
dialog.show();}
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
砍掉初始化代码让我很困惑,因为有一段时间注意到你的大括号不平衡......
看起来你正在创建你的计数器并在两个 onclick 函数之外初始化它。在此阶段长度为零。
当您单击开始按钮时,长度会设置为有效时间,但计时器不会重新初始化,因此它会以其之前设置的零值开始并立即触发 onFinish。
当您按下暂停时,将计算正确的剩余时间,然后将其传递到您在恢复时创建的新计数器,然后继续正常倒计时。
将
counter = new MyCount(length, 100);
直接移动到dialog.show();}
之后应确保在单击设置计时器按钮时计数器得到初始化并解决问题。Chopping off the initialisation code confused me there as it too a while to notice your braces didn't balance...
it looks like you are creating your counter and initialising it outside of both of the onclick functions. At this stage Length is zero.
when you click the start button length gets set to a valid time but the timer is not reinitialised so it starts with its previously set value of zero and fires onFinish straight away.
When you press pause the correct remaining time is calculated and which is then passed onto the new counter you create when you resume which then proceeds to count down normally.
Moving
counter = new MyCount(length, 100);
up to directly afterdialog.show();}
should ensure that the counter gets initialised when the set timer button is clicked and fix the problem.