如何设置Android活动的时间限制

时间:2014-03-30 23:46:53

标签: android button android-activity

我有一个游戏,如果用户触摸错误的按钮,他会进入高分页面,如果他点击了正确的按钮,他会进入下一个级别。我想做的是如果用户在1.5秒(快节奏游戏)中完全没有做任何事情就这样做,那么它会自动让他回到scores.class活动。我是编程新手,所以任何有用的东西!感谢。

3 个答案:

答案 0 :(得分:0)

这会给你一个想法:

private MainActivity context;
private CountDownTimer countDownTimer;
public boolean timerStopped;


/** Called when the activity is first created. */
@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    context = this;

    startTimer();

    // method looks at users choice, example
    /* if (answer == true){
        stopTimer();
        // go to next question and start timer again..
     }
     else{
        // do something
     }
    */
}

/** Starts the timer **/
public void startTimer() {
    setTimerStartListener();
    timerStopped = false;
}

/** Stop the timer **/
public void stopTimer() {
    countDownTimer.cancel();
    timerStopped = true;
}

/** Timer method: CountDownTimer **/
private void setTimerStartListener() {
    // will be called at every 1500 milliseconds i.e. every 1.5 second.
    countDownTimer = new CountDownTimer(1500, 1500) {
        public void onTick(long millisUntilFinished) {

        }

        public void onFinish() {
            // Here do what you like...
            Intent intent = new Intent(context, Scores.class);
            startActivity(intent);
        }
    }.start();
}

答案 1 :(得分:0)

你试过CountDownTimer吗?

以下是一个例子:

 new CountDownTimer(1500, 1500) {
        public void onTick(long millisUntilFinished) {

        }

        public void onFinish() {
           // Here do what you like...
        }
    }.start();

答案 2 :(得分:0)

修复上述错误:

 MainActivity context;

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    context = this;

    new CountDownTimer(1500, 1500) {
        public void onTick(long millisUntilFinished) {

        }

        public void onFinish() {
            // Here do what you like...
            Intent intent = new Intent(context, Score.class);
            startActivity(intent);
        }
    }.start();