我正在制作一个Android测验应用程序,除了添加问题之外似乎已经完成所有操作,但仍然存在很大的错误。 你看,我已经把随机生成器生成了4个问题。当我回答第四个时,我希望新的活动从分数的显示开始。而应用程序只是没有回应。我真的很无能。
谢谢!
package com.matej.hajdukkviz;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Glavno extends Activity implements OnClickListener {
int score = 0;
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 q1 = new Question(
"Q1",
"Correct answer - q1",
"Wrong answer 1 - q1",
"Wrong answer 2 - q1",
"Wrong answer 3 - q1"
);
Question q2 = new Question(
"Q2?",
"Correct answer - q2",
"Wrong answer 1 - q2",
"Wrong answer 2 - q2",
"Wrong answer 3 - q2"
);
Question q3 = new Question(
"Q3?",
"Correct answer - q3"
"Wrong answer 1 - q3",
"Wrong answer 2 - q3",
"Wrong answer 3 - q3"
);
Question q4 = new Question(
"Q4?",
"Correct answer - q4",
"Wrong answer 1 - q4",
"Wrong answer 2 - q4",
"Wrong answer 3 - q4"
);
@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);
// ADD THE QUESTIONS IN THE ArrayList qsts
qsts.add(q1);
qsts.add(q2);
qsts.add(q3);
qsts.add(q4);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);
countdown = (TextView) findViewById(R.id.countdown);
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(true){
int nxt = rng.nextInt(4);
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(buttonText.equals(nextQuestion.correctAnswerText)) {
textView2.setText("TOČNO!");
textView2.setTextColor(Color.GREEN);
textView3.setText("Rezultat: " + (score += 10));
allAnswers.clear();
generateQuestion();
return;
}else{
textView2.setText("NETOČNO!");
textView2.setTextColor(Color.RED);
textView3.setText("Rezultat: " + (score -= 5));
allAnswers.clear();
generateQuestion();
return;
}
}
}
答案 0 :(得分:0)
将一些内容放入下一个Activity
,确保在manifest.xml
中正确声明,为其创建布局,然后在上一个问题得到解答后添加此代码
Intent intent = new Intent(Glavno.this, NextActivityName.class); // declare and initialize an Intent object
startActivity(intent); // pass the Intent object to startActivity()
修改强>
我看到你已经接受了答案,但听起来它没有正常工作。您尚未完成,因此onClick()
中的代码会一直运行。我会编辑你的内容并在下面发布。这应该会有所帮助。
@Override
public void onClick(View v) {
Button b = (Button)v;
String buttonText = b.getText().toString();
if (counter == 4) {
startActivity(new Intent(Glavno.this, Score.class));
finish(); // Added this method call
}
else if(buttonText.equals(nextQuestion.correctAnswerText)) { // Changed this to else if
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;
}
}
答案 1 :(得分:0)
在你的generateQuestion()
循环中true
(总是如此),然后只有在未显示随机问题时执行某些操作。当然这是一个无限循环。如果是4个问题,您可以声明在每个问题后增加的全局变量counter
。而不是在true
时循环,而是说counter < 4
其他startNewIntent()
或类似于codeMagic提到的代码。
答案 2 :(得分:0)
我添加了这段代码,但它似乎没有用。我创建了一个变量counter = 0,你看到我放了什么代码..
@Override
public void onClick(View v) {
Button b = (Button)v;
String buttonText = b.getText().toString();
if (counter == 4) {
startActivity(new Intent(Glavno.this, Score.class));
}
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;
}
}