我正在使用Android Studio。我正在创建一个具有倒数计时器和按钮的应用程序。当我运行应用程序时,计时器会自动运行,如果我想单击按钮,则会有一个TextView计算我按下按钮的次数。我希望当我按下按钮时,计时器启动,倒计时继续运行并且它在时间“0:000”停止,因此它甚至停止点击计数。你能帮助我吗? (如果它有助于我放置代码)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCount = (TextView)findViewById(R.id.textView1);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.button1);
final TextView textViewTimer = (TextView)findViewById(R.id.textView2);
btnCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
count++;
txtCount.setText(String.valueOf(count));
}
});
new CountDownTimer(10000, 1) {
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished / 1000
+ ":" + millisUntilFinished % 1000);
}
public void onFinish() {
textViewTimer.setText("0:000");
}
}.start();
}
我不会为隐私编写其他代码的其余部分,但这是感兴趣的部分;)
答案 0 :(得分:1)
<强>被修改强>
您可以将链接保存到CountDownTimer并在任何地方使用它。对于您的问题,您可以有两个布尔变量:timerStarts
- 负责启动计时器一次,timerProcessing
- 负责检查计时器是否仍在处理。
像这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCount = (TextView)findViewById(R.id.textView1);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.button1);
boolean timerProcessing = false;
boolean timerStarts = false;
final TextView textViewTimer = (TextView)findViewById(R.id.textView2);
//Saving link to timer object
final CountDownTimer timer = new CountDownTimer(10000, 1) {
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished / 1000
+ ":" + millisUntilFinished % 1000);
}
public void onFinish() {
textViewTimer.setText("0:000");
timerProcessing = false;
}
});
btnCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//start timer once when button first click
if (!timerStarts){
timer.start();
timerStarts = true;
timerProcessing = true;
}
if (timerProcessing){
count++;
txtCount.setText(String.valueOf(count));
}
}
});
}
或者您可以拥有一个变量(例如timerStarts),在按钮侦听器中检查它并在计时器停止时设置为false,但是当您在计时器停止后单击该按钮时它将再次启动。 希望它有所帮助。
答案 1 :(得分:0)
如果我理解正确,您希望Button
在CoundTownTimer
完成时停止计算点击次数。为此,您可以在计时器结束时禁用Button
。
public void onFinish() {
textViewTimer.setText("0:000");
btnCount.setEnabled(false); // Add this line here
}
如果由于某种原因对您不起作用,那么您可以使用boolean
变量来决定是否添加到您的点数中。
boolean addToCount = true; // Create a variable
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCount = (TextView)findViewById(R.id.textView1);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.button1);
final TextView textViewTimer = (TextView)findViewById(R.id.textView2);
btnCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
count = (addToCount) ? count++ : count; // Add 1 to count if
// addToCount is true
txtCount.setText(String.valueOf(count));
}
new CountDownTimer(10000, 1) {
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished / 1000
+ ":" + millisUntilFinished % 1000);
}
public void onFinish() {
textViewTimer.setText("0:000");
addToCount = false; // Change variable so count doesn't increase
}
}.start();