点击等待片刻后显示imageview然后再次隐藏[Android]

时间:2015-06-16 18:05:08

标签: android multithreading imageview show-hide

我试图在用户点击按钮后显示一些图像视图,然后等待一段时间让用户看到哪个是正确的按钮,然后再次隐藏它。像这样:

用户点击按钮给出答案

- >通过将imageview的可见性设置为可见

,更新分数并向用户显示正确答案

- >等一下,让用户能够看到哪个是正确的答案

- >再次隐藏imageview并加载一个新问题。

到目前为止,我有这个:

private void gameLoop(){
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (isRunning) {
                if (time > 0) {
                    sleepmillis(1000);
                    time--;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textViewTime.setText(time + " sec.");
                        }
                    });
                } else
                    finishGame();
            }
        }
    }).start();
}

private void sleepmillis(int millis) {
    try {
        Thread.sleep(millis);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

@Override
public void onClick(View view) {
    int givenAnswer = -1;
    switch (view.getId()) {
        case R.id.buttonFirst:
        case R.id.imageViewFirst:
            givenAnswer = 0;
            break;
        case R.id.buttonSecond:
        case R.id.imageViewSecond:
            givenAnswer = 1;
            break;
        case R.id.buttonThird:
        case R.id.imageViewThird:
            givenAnswer = 2;
            break;
        case R.id.buttonFourth:
        case R.id.imageViewFourth:
            givenAnswer = 3;
            break;
    }
    processAnswer(givenAnswer);
}

private void processAnswer(int givenAnswer){
    if (givenAnswer == answers[4]){
        score += 5000;
        correctAnswersInARow++;
        if (correctAnswersInARow == 3)
            score += 15000;
        if (correctAnswersInARow == 6){
            score += 30000;
            correctAnswersInARow = 0;
        }
    }
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            textViewScore.setText("Score: " + score);
        }
    });
    showCorrectAnswer();
    answers = gameData.getAnswers();
    updateAnswers();
}

private void showCorrectAnswer(){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            switch (answers[4]){
                case 0:
                    ivCorrectFirst.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    ivCorrectSecond.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    ivCorrectThird.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    ivCorrectFourth.setVisibility(View.VISIBLE);
            }
        }
    });
    sleepmillis(400);
    hideCorrectAnswers();
}

private void hideCorrectAnswers(){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            ivCorrectFirst.setVisibility(View.INVISIBLE);
            ivCorrectSecond.setVisibility(View.INVISIBLE);
            ivCorrectThird.setVisibility(View.INVISIBLE);
            ivCorrectFourth.setVisibility(View.INVISIBLE);
        }
    });
}

所以重要的部分是在showCorrectAnswer方法中,我尝试做我在这里解释的。虽然应用程序在加载新问题之前等待400毫秒,但它没有显示正确的答案。

1 个答案:

答案 0 :(得分:1)

imageView.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
          imageView.setVisibility(View.INVISIBLE);
      }
    }, ms);

ms是持续时间(以毫秒为单位)。