package com.example.artru_000.question;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class questioninterface extends ActionBarActivity {
public static final int QUESTIONS_LENGTH = 50;
public void setQuestion() {
for (Questions e : Questions.values()) {
if (random(QUESTIONS_LENGTH) == e.order) {
currentQuestion = e.question;
correctAnswer = e.correctAnswer;
answer1 = e.answer1;
answer2 = e.answer2;
clearWidgetsText();
setWidgetsText();
return;
}
}
}
public void clearWidgetsText() {
firstAnswer.setText("");
secondAnswer.setText("");
thirdAnswer.setText("");
question.setText("");
}
public void setWidgetsText() {
question.setText(currentQuestion);
switch (random(2)) {
case 0:
firstAnswer.setText(correctAnswer);
secondAnswer.setText(answer1);
thirdAnswer.setText(answer2);
break;
case 1:
thirdAnswer.setText(correctAnswer);
secondAnswer.setText(answer1);
firstAnswer.setText(answer2);
break;
case 2:
secondAnswer.setText(correctAnswer);
firstAnswer.setText(answer1);
thirdAnswer.setText(answer2);
break;
}
}
Random rand = new Random();
String currentQuestion, correctAnswer, answer1, answer2;
public int random(int range) {
return rand.nextInt(range);
}
Button getQuestion;
Button firstAnswer;
Button secondAnswer;
Button thirdAnswer;
TextView answerStatus;
TextView question;
public void setAnswerStatus(String status) {
if(status.equalsIgnoreCase("Right")) {
answerStatus.setText("Right");
answerStatus.setTextColor(Color.GREEN);
}
if(status.equalsIgnoreCase("wrong")) {
answerStatus.setText("Wrong");
answerStatus.setTextColor(Color.RED);
}
}
public void buttonPushes() {
getQuestion.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setQuestion();
return;
}
});
firstAnswer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(firstAnswer.getText().toString().equalsIgnoreCase(correctAnswer)) {
setAnswerStatus("right");
setQuestion();
return;
} else {
setAnswerStatus("wrong");
setQuestion();
return;
}
}
});
secondAnswer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(secondAnswer.getText().toString().equalsIgnoreCase(correctAnswer)) {
setAnswerStatus("right");
setQuestion();
return;
} else {
setAnswerStatus("wrong");
setQuestion();
return;
}
}
});
thirdAnswer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(thirdAnswer.getText().toString().equalsIgnoreCase(correctAnswer)) {
setAnswerStatus("right");
setQuestion();
return;
} else {
setAnswerStatus("wrong");
setQuestion();
return;
}
}
});
}
private void construct() {
getQuestion = (Button) findViewById(R.id.get_question);
firstAnswer = (Button) findViewById(R.id.answer1);
secondAnswer = (Button) findViewById(R.id.answer2);
thirdAnswer = (Button) findViewById(R.id.answer3);
answerStatus = (TextView) findViewById(R.id.wrongright);
question = (TextView) findViewById(R.id.question);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questioninterface);
construct();
buttonPushes();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_questioninterface, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
上面的代码就像一个琐事游戏。当您单击 getQuestion 按钮时,您会得到一个问题和三个按钮,其中包含3个不同的答案。如果单击右键,则说它是正确的,并为您提供另一个问题。如果你回答错了,它会说错了,并给你另一个问题。有用。但问题是,有时我需要多次点击按钮才能得到另一个问题和其他答案。有什么想法吗?
答案 0 :(得分:0)
您是否尝试在代码中添加断点并在调试模式下运行它以确定发生奇怪的地方?在按钮点击中添加断点,您可以在哪里决定要做什么,输出信息的位置等等。如果有,请告诉我们您的看法。