【问题标题】:How to pause an activity to show the correct answer in a quiz?如何暂停活动以在测验中显示正确答案?
【发布时间】:2013-09-10 02:17:16
【问题描述】:

HY, 好吧,我正在做一个 Android 测验,正如您在单击按钮后从代码中看到的那样,它会自动生成一个新问题,但我希望它保持这样一秒钟,如果正确则变为绿色,如果错误则变为红色..我不知道如何让它暂停,也许是 sleep() 方法..谢谢!

public class Glavno extends Activity implements OnClickListener {

int score  = 0;
int counter = 0;
boolean ajme = true;

TextView textView1, textView2, textView3, countdown;
Button btn1, btn2, btn3, btn4;

ArrayList<Question> qsts = new ArrayList<Question>();
List<Integer> generated = new ArrayList<Integer>();

ArrayList<String> allAnswers = new ArrayList<String>();

Random rng = new Random();
Question nextQuestion;

Question qjedan = new Question(
    "Q1",

    "Correct answer - q1",
    "Wrong answer 3 - q1",
    "Wrong answer 3 - q1",
    "Wrong answer 3 - q1"
    );
Question q2 = new Question(
    "Q2",

    "Correct answer - q2",
    "Wrong answer 3 - q2",
    "Wrong answer 3 - q2",
    "Wrong answer 3 - q2"
    );
Question q3 = new Question(
    "Q3",

    "Correct answer - q3",
    "Wrong answer 3 - q3",
    "Wrong answer 3 - q3",
    "Wrong answer 3 - q3"
    );


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.pitanja);

 new CountDownTimer(20000, 1000) {

     public void onTick(long millisUntilFinished) {
         textView4.setText("Seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         generateQuestion();
         textView2.setText("VRIJEME!");
         textView2.setTextColor(Color.RED);

     }
      }.start();

qsts.add(qjedan);           
qsts.add(q2);
qsts.add(q3);

textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);

textView3.setText("Rezultat: " + score);

    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);
    btn3 = (Button) findViewById(R.id.btn3);
    btn4 = (Button) findViewById(R.id.btn4);

    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);

generateQuestion();

}

public void generateQuestion(){

    while(ajme = true){

        int nxt = rng.nextInt(3);

        if (!generated.contains(nxt)){

            generated.add(nxt);

            nextQuestion = qsts.get(nxt);

            textView1.setText(nextQuestion.questionText);

            allAnswers.add(nextQuestion.correctAnswerText);
            allAnswers.add(nextQuestion.wrongAnswer1);
            allAnswers.add(nextQuestion.wrongAnswer2);
            allAnswers.add(nextQuestion.wrongAnswer3);

            Collections.shuffle(allAnswers);

            btn1.setText(allAnswers.get(0));
            btn2.setText(allAnswers.get(1));
            btn3.setText(allAnswers.get(2));
            btn4.setText(allAnswers.get(3));

            break;
        }
    }
}


@Override
public void onClick(View v) {
    Button b = (Button)v;
    String buttonText = b.getText().toString();

    if (counter == 3) {

        Intent theIntent = new Intent(this, Score.class);
        theIntent.putExtra("somename", score);  
        startActivity(theIntent);

        finish();   // Added this method call
    }

    else if(buttonText.equals(nextQuestion.correctAnswerText)) { 

        counter++;

        AdView ad = (AdView) findViewById(R.id.ads);
        ad.loadAd(new AdRequest());

        textView2.setText("TOČNO!");
        textView2.setTextColor(Color.GREEN);
        textView3.setText("Rezultat: " + (score += 10));

        allAnswers.clear();
        generateQuestion();

        return;
    } 

    else{

        counter++;

        AdView ad = (AdView) findViewById(R.id.ads);
        ad.loadAd(new AdRequest());

        textView2.setText("NETOČNO!");
        textView2.setTextColor(Color.RED);
        textView3.setText("Rezultat: " + (score -= 5));

        allAnswers.clear();
        generateQuestion();

        return; 
    }

}   

【问题讨论】:

  • 您没有将正确答案标记为正确,因此我这边没有更多答案。祝你好运找到其他人在你的问题上浪费时间。
  • oo 对不起兄弟。我刚看到那条评论,不用担心,我将其标记为正确,谢谢。

标签: android


【解决方案1】:

不要让您的 UI 线程阻塞/暂停,而是使用 postDelayed。这样,您的操作会在一段时间后被调用,而不会阻塞任何内容。


来自您的 UI 线程:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
      // do your timed work here, e.g. switch to the next question
    }
  }, 1000); // 1000 milliseconds = 1 second

【讨论】:

  • oo,你能不能放一个使用这种方法的代码示例,因为我不太懂,因为英语不是我的主要语言。谢谢!
  • 非常感谢您花时间帮助我,您知道有什么方法可以将持有正确答案的按钮变为绿色吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-24
  • 1970-01-01
相关资源
最近更新 更多