我的琐事应用程序出现问题,它可以显示第一个问题但是当我点击答案(按钮)时没有任何反应。单击答案按钮时,用户将获得分数并将添加总答案。但是,如果用户选择了错误的答案,用户仍然会转到下一个问题。
public class play extends Activity implements OnClickListener {
private Question currentQuestion;
private int currentQuestionIndex;
private ArrayList<Button> questionButton;
private TextView questionstextview;
private TextView questionnumber;
private TextView playerfeedback;
public static TextView displayscore;
public static int score;
private List<Question> QuestionList;
private int answerchoice;
public static int totalanswer;
public static int correctanswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
Log.i("playclass", "this is play class running");
questionButton = new ArrayList<Button>();
questionButton.add((Button) findViewById (R.id.answerbutton1));
questionButton.add((Button) findViewById (R.id.answerbutton2));
questionButton.add((Button) findViewById (R.id.answerbutton3));
questionButton.add((Button) findViewById (R.id.answerbutton4));
currentQuestion = null;
currentQuestionIndex = 0;
View AnswerButton1 = findViewById(R.id.answerbutton1);
AnswerButton1.setOnClickListener(this);
View AnswerButton2 = findViewById(R.id.answerbutton2);
AnswerButton2.setOnClickListener(this);
View AnswerButton3 = findViewById(R.id.answerbutton3);
AnswerButton3.setOnClickListener(this);
View AnswerButton4 = findViewById(R.id.answerbutton4);
AnswerButton4.setOnClickListener(this);
Log.i("playclass", "aftersetlistener");
QuestionList = new ArrayList<Question>();
ArrayList <String> answer = new ArrayList<String>();
answer.add("8");
answer.add("9");
answer.add("3");
answer.add("1");
QuestionList.add(new Question("what is 4+4", answer, 0));
answer.add("17");
answer.add("20");
answer.add("15");
answer.add("14");
QuestionList.add(new Question("what is 7+8?", answer, 3));
answer.add("20");
answer.add("30");
answer.add("19");
answer.add("34");
QuestionList.add(new Question("what is 10+10?", answer, 0));
answer.add("12");
answer.add("11");
answer.add("13");
answer.add("14");
QuestionList.add(new Question("what is 6+6?", answer, 0));
answer.add("6");
answer.add("5");
answer.add("4");
answer.add("7");
QuestionList.add(new Question("what is 4+3?", answer, 3));
answer.add("7");
answer.add("9");
answer.add("10");
answer.add("11");
QuestionList.add(new Question("what is 3+7?", answer, 2));
questionstextview = (TextView) findViewById (R.id.questionstextview);
questionnumber = (TextView) findViewById (R.id.questionnumber);
displayscore = (TextView) findViewById (R.id.displayscore);
StartTrivia();
}
public void ButtonPress (View answerButton){
Log.i("playclass", "after View answerButton ");
MediaPlayer soundfx = MediaPlayer.create(getApplicationContext(), R.raw.click);
soundfx.start();
Log.i("playclass", "after soundfx ");
for (int i=0; i< questionButton.size(); i++)
if (questionButton.get(i) ==answerButton)
if (i==currentQuestion.getAnswerIndex()){
Log.i("playclass", "before adding score,total answer and correctanswer ");
score=+5;
totalanswer++;
correctanswer++;
Log.i("playclass", "after adding score,total answer and correctanswer ");
displayscore.setText(Integer.toString(score));
Log.i("playclass", "after display score/setscore to integer ");
Toast.makeText(getApplicationContext(), "Correct!!", Toast.LENGTH_SHORT).show();
Log.i("playclass", "after correct toast ");
}
else{
Toast.makeText(getApplicationContext(), "Incorrect!!", Toast.LENGTH_SHORT).show();
Log.i("playclass", "after incorrect toast");
}
currentQuestionIndex++;
Log.i("playclass", "after currentQuestionIndex++; ");
if (currentQuestionIndex < QuestionList.size()){
Log.i("playclass", "after currentQuestionIndex < QuestionList.size() ");
StartTrivia();
Log.i("playclass", "after StartTrivia in if statement ");
}
else{
Intent result = new Intent (this, finalscreen.class);
startActivity(result);
Log.i("playclass", "after IntentResult ");
}
}
public void StartTrivia(){
Log.i("playclass", "running StartTrivia() " + currentQuestion);
currentQuestion = QuestionList.get(currentQuestionIndex);
Log.i("playclass", "after get current question " + currentQuestion);
questionstextview.setText(currentQuestion.getquestion());
Log.i("playclass", "after set current question");
questionnumber.setText(Integer.toString(currentQuestionIndex+1));
Log.i("playclass", "after convert int to string for question number");
for (int i = 0; i < questionButton.size(); i++)
{
Log.i("playclass", "before get question button");
Log.i("playclass", "before currentQuestion " + i + currentQuestion.getanswer());
String ans = currentQuestion.getanswer().get(i);
questionButton.get(i).setText(ans);
Log.i("playclass", "after get question button");
}
} <br><br>
播放XML按钮
<Button
android:id="@+id/answerbutton1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ButtonPress"
android:text="Button" />
<Button
android:id="@+id/answerbutton2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ButtonPress"
android:text="Button" />
<Button
android:id="@+id/answerbutton3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ButtonPress"
android:text="Button" />
<Button
android:id="@+id/answerbutton4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ButtonPress"
android:text="Button" />
</LinearLayout>
</LinearLayout><br><br>
LogCat
08-12 14:11:26.853:I / playclass(330):这是运行类的运行类
08-12 14:11:26.853:I / playclass(330):aftersetlistener
08-12 14:11:26.873:I / playclass(330):运行StartTrivia()null
08-12 14:11:26.873:I / playclass(330):在得到当前问题后com.example.quizgame.Question@408feec8
08-12 14:11:26.873:I / playclass(330):设定当前问题后
08-12 14:11:26.873:I / playclass(330):将int转换为字符串后转到问题编号
08-12 14:11:26.873:I / playclass(330):在得到问题按钮之前
08-12 14:11:26.873:I / playclass(330):在currentQuestion 0之前[8,9,3,1,17,20,15,14,20,30,19,34,12,11,13, 14,16,5,4,7,7,9,10,11]
08-12 14:11:26.883:I / playclass(330):获得问题按钮后
08-12 14:11:26.883:I / playclass(330):在得到问题按钮之前
08-12 14:11:26.883:I / playclass(330):在当前问题1之前[8,9,3,1,17,20,15,14,20,30,19,34,12,11,13, 14,16,5,4,7,7,9,10,11]
08-12 14:11:26.883:I / playclass(330):获得问题按钮后
08-12 14:11:26.883:I / playclass(330):在得到问题按钮之前
08-12 14:11:26.883:I / playclass(330):在当前问题2之前[8,9,3,1,17,20,15,14,20,30,19,34,12,11,13, 14,16,5,4,7,7,9,10,11]
08-12 14:11:26.883:I / playclass(330):获得问题按钮后
08-12 14:11:26.883:I / playclass(330):在得到问题按钮之前
08-12 14:11:26.893:I / playclass(330):在当前问题3之前[8,9,3,1,17,20,15,14,20,30,19,34,12,11,13, 14,16,5,4,7,7,9,10,11]
08-12 14:11:26.893:I / playclass(330):获得问题按钮后
08-12 14:11:27.423:I / ActivityManager(74):显示com.example.quizgame / .play:+ 722ms
答案 0 :(得分:0)
请不要使用
android:onClick="ButtonPress"
来自XML文件。
如果您想使用多个按钮,请使用以下方式:
public class MainActivity extends Activity implements OnClickListener {
private final static String TAG = MainActivity.class.getSimpleName();
Button sendButton, otherButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendButton = (Button) findViewById(R.id.sendButton);
sendButton.setOnClickListener(this);
otherButton = (Button) findViewById(R.id.otherButton);
otherButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sendButton:
// do something with the button click
break;
case R.id.otherButton:
// do something
break;
}
}
}
这样可以避免出现奇怪的行为。
答案 1 :(得分:0)
如果您在XML中使用android:onclick
,则无需在代码中为按钮设置OnClickListener。删除代码中的所有setOnClickListener()
内容以及implements OnClickListener
,它应该有效。
答案 2 :(得分:0)
你不需要使用android:onClick =“ButtonPress”
您可以直接为每个按钮使用以下代码
answerbutton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//your code for the particular button
}
});