由于我是Android的新手,请帮帮我。在我的应用程序中,当我点击文本框时,它开始计算点击次数,并在倒数计时器启动时点击文本框。我希望我的计数器(mCount)停止当CountDown计时器结束时我也希望当计时器完成时没有人可以点击文本框意味着文本框变得不可点击,直到用户按下重置按钮。提前谢谢!
这是我的代码: TextView tv,t1;
Button reset;
private int mCount = 0;
private final long startTime = 5 * 1000;
private final long interval = 1 * 1000;
private CountDownTimer countDownTimer;
private boolean timerStarted = false;
private MediaRouteButton textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tv = (TextView)findViewById(R.id.tv1);
tv.setText("5"); // starting from 5.
reset=(Button)findViewById(R.id.reset);
final TextView countTextView = (TextView) findViewById(R.id.TextViewCount);
t1=(TextView)findViewById(R.id.textView2);
countDownTimer = new MyCountDownTimer(startTime, interval);
t1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!timerStarted) {
countDownTimer.start();
timerStarted = true;
}
mCount++;
countTextView.setText("SCORE: " + mCount);
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
}
public class MyCountDownTimer extends CountDownTimer {
private TextView mTextField;
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
mTextField.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
tv.setText("" + millisUntilFinished / 1000);
}
}
}
答案 0 :(得分:0)
当计时器结束时,您可以暂时使用View.setClickable(boolean)
禁用点击:
public void onFinish(){
mTextField.setText("done!");
t1.setClickable(false);
}
只要最方便重新启动计时器,您就可以更改可点击状态。
答案 1 :(得分:0)
只保持在Timer停止时和递增计数器检查该标志为假或不是
之前为真的标志