我如何可以隨機將與RadioButton
顯示的問題。我看了其他帖子,但答案不適合我。的Android測驗隨機出題
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class QuizActivity extends Activity {
int score = 0;
int qnum = 1;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button butNext;
RadioGroup rdgrp;
String corAnswer = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_quiz);
DbHelper db = new DbHelper(this);
rdgrp = (RadioGroup) findViewById(R.id.questionAndAnswers);
txtQuestion = (TextView) findViewById(R.id.textView1);
rda = (RadioButton) findViewById(R.id.radio0);
rdb = (RadioButton) findViewById(R.id.radio1);
rdc = (RadioButton) findViewById(R.id.radio2);
rdd = (RadioButton) findViewById(R.id.radio3);
butNext = (Button) findViewById(R.id.button1);
corAnswer = "";
onCreateQuestion();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_quiz, menu);
return true;
}
public void onCreateQuestion() {
String level = getIntent().getExtras().getString("level");
DbHelper db = new DbHelper(this);
db.getQuestByLevel(level, qnum);
txtQuestion.setText(db.question);
rda.setText(db.optionA);
rdb.setText(db.optionB);
rdc.setText(db.optionC);
rdd.setText(db.optionD);
corAnswer = db.answer;
qnum++;
}
public void onClickNext(View view) {
String level = getIntent().getExtras().getString("level");
DbHelper db = new DbHelper(this);
db.getQuestByLevel(level, qnum);
RadioGroup grp = (RadioGroup) findViewById(R.id.questionAndAnswers);
RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
if (answer == null)
{
Toast.makeText(QuizActivity.this, "select an answer please", Toast.LENGTH_SHORT).show();
return;
}
if (corAnswer!= null && corAnswer.equalsIgnoreCase((String) answer.getText()))
{
score++;
Log.d("answer", "Your score" + score);
}
if (qnum <= 5) {
} else {
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score);
intent.putExtras(b);
startActivity(intent);
finish();
}
txtQuestion.setText(db.question);
rda.setText(db.optionA);
rdb.setText(db.optionB);
rdc.setText(db.optionC);
rdd.setText(db.optionD);
corAnswer = db.answer;
qnum++;
rdgrp.clearCheck();
}
}
什麼是您的具體問題?你看過哪些帖子,哪些不起作用? – PKeno
這裏沒有問題。我只需要證明隨機抽題 – Cyril
@Cyril如果沒有問題,那麼毫無疑問 –