[英]I'm using JTabbedPane for my quiz program, not sure how to get my buttons to display the answers
[英]I've created a quiz however I'm not sure how to implement the capturing of the buttons pressed to check against the answer using Parse
我有四個按鈕和一個textView。 使用以下代碼從Parse.com中檢索textView中的文本:
final ParseQuery<ParseObject> quizOne = ParseQuery.getQuery("Quiz");
quizOne.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject reqDetails, ParseException e) {
if (quizOne != null) {
Log.d("quizOne", "Got it");
//Retrieve Age
String gettheQuestion = reqDetails.getString("questionOne");
TextView getQone = (TextView) findViewById(R.id.quesOne);
getQone.setText(gettheQuestion);
Toast.makeText(getApplicationContext(),
"Successfully Recieved Question",
Toast.LENGTH_LONG).show();
} else {
Log.d("quizOne", "Question not retreived.");
Toast.makeText(getApplicationContext(),
"Can't Get Details, Check Connection", Toast.LENGTH_LONG)
.show();
}
}
});
這是按鈕的代碼:
optionAq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (optionAq.equals(optionAq)) {
Intent intent = new Intent(Quiz.this, Question2.class);
startActivity(intent);
}
}
});
optionBq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Quiz.this, Question2.class);
startActivity(intent);
}
});
optionCq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Quiz.this, Question2.class);
startActivity(intent);
}
});
optionDq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Quiz.this, Question2.class);
startActivity(intent);
}
});
我希望能夠捕獲所按下的按鈕,並在之后像結果屏幕一樣顯示。 任何指針,我將如何做到這一點?
謝謝。
您不需要所有這些onClickListeners
。 您只需要一個帶有多個RadioButtons
的RadioGroup
。 您必須將其包含在XML布局中,如下所示:
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:orientation="vertical" >
<RadioButton
android:id="@+id/optionAq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="A" />
<RadioButton
android:id="@+id/optionAb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="B" />
<RadioButton
android:id="@+id/optionCb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="C" />
<RadioButton
android:id="@+id/optionDb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="D" />
</RadioGroup>
然后,在“ Activity
,必須將“ Buttons
更改為“ RadioButtons
Buttons
。
然后添加方法onRadioButtonClicked()
,如果選擇了任何RadioButtons
,則將調用該方法。
public void onRadioButtonClicked(View v){
Log.i("Answer", "Answer is "+ ((RadioButton)v).getText());
Intent intent = new Intent(Quiz.this, Question2.class);
//send the answer to the next Activity if yo need
intent.putExtra("answer", ((RadioButton)v).getText());
//start next Activity
startActivity(intent);
}
根據您的實現,即在單擊RadioButton
啟動一個新的Activity
,這是一個很好的方法。
還有許多其他方法,例如使用radioGroup1.getCheckedRadioButtonId()
獲取選定的RadioButton
ID。
無論如何,您可以在我之前的評論中提供的URL中了解到更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.