Java 我制作了一个测验应用程序,我想保存为问题选择的选项,并在我的复习活动中使用它们
我制作了一个测验应用程序,我想保存我为问题选择的选项,并在我的复习活动中使用它们,但我不知道如何做到这一点,看看是否有人能提供帮助 以下是我的测验活动代码:Java 我制作了一个测验应用程序,我想保存为问题选择的选项,并在我的复习活动中使用它们,java,android,android-intent,android-activity,sharedpreferences,Java,Android,Android Intent,Android Activity,Sharedpreferences,我制作了一个测验应用程序,我想保存我为问题选择的选项,并在我的复习活动中使用它们,但我不知道如何做到这一点,看看是否有人能提供帮助 以下是我的测验活动代码: public class NumberSystem1Activity extends AppCompatActivity { private RadioGroup radioGroup; private NumberSystem1QuestionBank mNumberSystem1QuestionBank = new
public class NumberSystem1Activity extends AppCompatActivity {
private RadioGroup radioGroup;
private NumberSystem1QuestionBank mNumberSystem1QuestionBank = new NumberSystem1QuestionBank();
private TextView mScoreView;
private TextView times;//For Timer in Quiz
private TextView mQuestionView;
private ImageView mQuestionImageView;
private RadioButton mButtonChoice1;
private RadioButton mButtonChoice2;
private RadioButton mButtonChoice3;
private RadioButton mButtonChoice4;
private String mAnswer;
private int mScore = 0;
private int mQuestionNumber = 0;
private int count_correct_answer =0;
private int count_wrong_answer=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testactivity1);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.clearCheck();
mScoreView = (TextView) findViewById(R.id.score);
mQuestionImageView = (ImageView)findViewById(R.id.image);
mQuestionView = (TextView) findViewById(R.id.question);
mButtonChoice1 = (RadioButton) findViewById(R.id.choice1);
mButtonChoice2 = (RadioButton) findViewById(R.id.choice2);
mButtonChoice3 = (RadioButton) findViewById(R.id.choice3);
mButtonChoice4 = (RadioButton) findViewById(R.id.choice4);
final Button go = (Button) findViewById(R.id.button2);
updateQuestion();
// Set the timer for Quiz
times = (TextView) findViewById(R.id.timers);
// method which will set the things up for our game
times.setText("00:02:00");
// A timer of 20 minutes to play for, with an interval of 1 second (1000 milliseconds)
NumberSystem1Activity.CounterClass timer = new NumberSystem1Activity.CounterClass(30000, 1000);
timer.start();
//Code Ends here
go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mButtonChoice1.isChecked()) {
if (mButtonChoice1.getText() == mAnswer) {
//Post Question Delay Code
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
//Code Ends hers
updateQuestion();
}
};
handle.postDelayed(delay, 2000);
mScore = mScore + 3;
updateScore(mScore);
count_correct_answer++;
mButtonChoice1.setBackgroundColor(Color.GREEN);
//This line of code is optional
Toast.makeText(NumberSystem1Activity.this, "Correct Answer", Toast.LENGTH_SHORT).show();
}
else {
//Post Question Delay Code
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
//Code Ends hers
updateQuestion();
}
};
handle.postDelayed(delay, 2000);
mScore = (mScore - 1);
count_wrong_answer++;
updateScore(mScore);
mButtonChoice1.setBackgroundColor(Color.RED);
//This line of code is optional
Toast.makeText(NumberSystem1Activity.this, "Wrong Answer", Toast.LENGTH_SHORT).show();
}
} else if (mButtonChoice2.isChecked()) {
if (mButtonChoice2.getText() == mAnswer) {
//Post Question Delay Code
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
//Code Ends hers
updateQuestion();
}
};
handle.postDelayed(delay, 2000);
mScore = mScore + 3;
updateScore(mScore);
count_correct_answer++;
mButtonChoice2.setBackgroundColor(Color.GREEN);
//This line of code is optional
Toast.makeText(NumberSystem1Activity.this, "Correct Answer", Toast.LENGTH_SHORT).show();
}
else {
//Post Question Delay Code
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
//Code Ends hers
updateQuestion();
}
};
handle.postDelayed(delay, 2000);
mScore = (mScore - 1);
count_wrong_answer++;
updateScore(mScore);
mButtonChoice2.setBackgroundColor(Color.RED);
//This line of code is optional
Toast.makeText(NumberSystem1Activity.this, "Wrong Answer", Toast.LENGTH_SHORT).show();
}
} else if (mButtonChoice3.isChecked()) {
if (mButtonChoice3.getText() == mAnswer) {
//Post Question Delay Code
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
//Code Ends hers
updateQuestion();
}
};
handle.postDelayed(delay, 2000);
mScore = mScore + 3;
updateScore(mScore);
count_correct_answer++;
mButtonChoice3.setBackgroundColor(Color.GREEN);
//This line of code is optional
Toast.makeText(NumberSystem1Activity.this, "Correct Answer", Toast.LENGTH_SHORT).show();
}
else {
//Post Question Delay Code
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
//Code Ends hers
updateQuestion();
}
};
handle.postDelayed(delay, 2000);
mScore = (mScore - 1);
count_wrong_answer++;
updateScore(mScore);
mButtonChoice3.setBackgroundColor(Color.RED);
//This line of code is optional
Toast.makeText(NumberSystem1Activity.this, "Wrong Answer", Toast.LENGTH_SHORT).show();
}
} else if (mButtonChoice4.isChecked()) {
if (mButtonChoice4.getText() == mAnswer) {
//Post Question Delay Code
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
//Code Ends hers
updateQuestion();
}
};
handle.postDelayed(delay, 2000);
mScore = mScore + 3;
updateScore(mScore);
count_correct_answer++;
mButtonChoice4.setBackgroundColor(Color.GREEN);
//This line of code is optional
Toast.makeText(NumberSystem1Activity.this, "Correct Answer", Toast.LENGTH_SHORT).show();
}
else {
//Post Question Delay Code
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
//Code Ends hers
updateQuestion();
}
};
handle.postDelayed(delay, 2000);
mScore = (mScore - 1);
count_wrong_answer++;
updateScore(mScore);
mButtonChoice4.setBackgroundColor(Color.RED);
//This line of code is optional
Toast.makeText(NumberSystem1Activity.this, "Wrong Answer", Toast.LENGTH_SHORT).show();
}
}
}
});
}
public void onClear(View v) {
/* Clears all selected radio buttons to default */
radioGroup.clearCheck();
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
@Override
public void onFinish() {
times.setText("Time is up");
Intent intent = new Intent(NumberSystem1Activity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("mScore", + mScore); // Your score
intent.putExtras(b); // Put your score to your next
Bundle c = new Bundle();
c.putInt("count_correct_answer", + count_correct_answer); // Your score
intent.putExtras(c); // Put your Correct Answer score to your next
Bundle d = new Bundle();
d.putInt("count_wrong_answer", + count_wrong_answer); // Your score
intent.putExtras(d); // Put your Wrong Answer score to your next
startActivity(intent);
finish();
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
long millis = millisUntilFinished;
String hms = String.format(
"%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(millis)));
System.out.println(hms);
times.setText(hms);
}
}
private void updateQuestion(){
if (mQuestionNumber< 30) {
// if questions are not over then do this
mQuestionView.setText(mNumberSystem1QuestionBank.getQuestion(mQuestionNumber));
mQuestionImageView.setImageResource(mNumberSystem1QuestionBank.getQuestionImages(mQuestionNumber));
mButtonChoice1.setText(mNumberSystem1QuestionBank.getChoice1(mQuestionNumber));
mButtonChoice2.setText(mNumberSystem1QuestionBank.getChoice2(mQuestionNumber));
mButtonChoice3.setText(mNumberSystem1QuestionBank.getChoice3(mQuestionNumber));
mButtonChoice4.setText(mNumberSystem1QuestionBank.getChoice4(mQuestionNumber));
mAnswer = mNumberSystem1QuestionBank.getCorrectAnswer(mQuestionNumber);
radioGroup.clearCheck();
mQuestionNumber++;
//set all buttons Blue for next question
mButtonChoice1.setBackgroundResource(R.color.blue_light);
mButtonChoice2.setBackgroundResource(R.color.blue_dark);
mButtonChoice3.setBackgroundResource(R.color.blue_light);
mButtonChoice4.setBackgroundResource(R.color.blue_dark);
}
else {
// if over do this
Intent intent = new Intent(NumberSystem1Activity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("mScore", mScore); // Your score
intent.putExtras(b); // Put your score to your next
Bundle c = new Bundle();
c.putInt("count_correct_answer", + count_correct_answer); // Your score
intent.putExtras(c); // Put your Correct Answer score to your next
Bundle d = new Bundle();
d.putInt("count_wrong_answer", + count_wrong_answer); // Your score
intent.putExtras(d); // Put your Wrong Answer score to your next
startActivity(intent);
finish();
}
}
private void updateScore(int point){
mScoreView.setText("" + mScore);
}
Here is my Review Activity code:
public class ReviewActivity1 extends AppCompatActivity {
private NumberSystem1QuestionBank mNumberSystem1QuestionBank = new NumberSystem1QuestionBank();
private TextView mQuestionView;
private ImageView mQuestionImageView;
private TextView mExplanationView;
private ImageView mExplanationImageView;
private TextView mCorrectAnswer;
private int mQuestionNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_review);
mQuestionView = (TextView) findViewById(R.id.question);
mQuestionImageView = (ImageView)findViewById(R.id.image);
mExplanationView = (TextView) findViewById(R.id.ExplanationText);
mExplanationImageView = (ImageView)findViewById(R.id.photo);
mCorrectAnswer = (TextView) findViewById(R.id.CorrectAnswerText);
final Button pre = (Button) findViewById(R.id.previous);
final Button next = (Button) findViewById(R.id.next);
updateQuestion();
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mQuestionNumber<30){
mQuestionNumber++;
updateQuestion();
}
else {
Intent intent = new Intent(ReviewActivity1.this, MainActivity.class);
startActivity(intent);
finish();
}
}
});
//if you want to disable the back button on the first question
pre.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mQuestionNumber>0) {
mQuestionNumber--;
updateQuestion();
} else {
Toast.makeText(ReviewActivity1.this, "This is the First Question. No Previous One.", Toast.LENGTH_SHORT).show();
// you're at the first question => no previous one
}
}
});
}
private void updateQuestion() {
if (mQuestionNumber < 30) {
// if questions are not over then do this
mQuestionView.setText(mNumberSystem1QuestionBank.getQuestion(mQuestionNumber));
mQuestionImageView.setImageResource(mNumberSystem1QuestionBank.getQuestionImages(mQuestionNumber));
mExplanationView.setText(mNumberSystem1QuestionBank.getExplanations(mQuestionNumber));
mExplanationImageView.setImageResource(mNumberSystem1QuestionBank.getExplanationImages(mQuestionNumber));
mCorrectAnswer.setText(mNumberSystem1QuestionBank.getCorrectAnswer(mQuestionNumber));
} else {
Intent intent = new Intent(ReviewActivity1.this, MainActivity.class);
startActivity(intent);
finish();
}
}
@Override
public void onBackPressed(){
//Exit The Quiz Alert Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(ReviewActivity1.this);
builder.setMessage("Do you want to Exit the Review." );
builder.setCancelable(true);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(ReviewActivity1.this, MainActivity.class);
startActivity(intent);
Toast.makeText(ReviewActivity1.this, "Test Page Appeared.", Toast.LENGTH_SHORT).show();
finish();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
public class numbersystem1活动扩展了AppCompative活动{
私人放射组放射组;
private NumberSystem1QuestionBank mNumberSystem1QuestionBank=新号码System1QuestionBank();
私有文本视图mScoreView;
私有TextView时间;//用于测验中的计时器
私有文本视图mQuestionView;
私有图像视图mQuestionImageView;
专用单选按钮MButonchoice1;
私人无线按钮mButtonChoice2;
专用单选按钮MButonchoice3;
专用单选按钮mButtonChoice4;
私人字符串mAnswer;
私有int mScore=0;
私有int mQuestionNumber=0;
私有整数计数\u正确\u答案=0;
私有整数计数\u错误\u答案=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.testactivity1);
放射组=(放射组)findViewById(R.id.radioGroup);
radioGroup.clearCheck();
mScoreView=(TextView)findViewById(R.id.score);
mQuestionImageView=(ImageView)findviewbyd(R.id.image);
mQuestionView=(TextView)findViewById(R.id.question);
mButtonChoice1=(单选按钮)findViewById(R.id.choice1);
mButtonChoice2=(单选按钮)findViewById(R.id.choice2);
mButtonChoice3=(单选按钮)findViewById(R.id.choice3);
mButtonChoice4=(单选按钮)findViewById(R.id.choice4);
最终按钮go=(按钮)findViewById(R.id.button2);
updateQuestion();
//设置测验的计时器
times=(TextView)findViewById(R.id.timers);
//为我们的游戏做准备的方法
times.setText(“00:02:00”);
//播放时间为20分钟的计时器,间隔为1秒(1000毫秒)
NumberSystem1Activity.antersass计时器=新的NumberSystem1Activity.antersass(300001000);
timer.start();
//代码到此结束
go.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(mButtonChoice1.isChecked()){
if(mButtonChoice1.getText()==mAnswer){
//问题后延迟码
最终处理程序句柄=新处理程序();
可运行延迟=新的可运行(){
公开募捐{
//代码以她的结尾
updateQuestion();
}
};
处理延迟(延迟,2000年);
mScore=mScore+3;
更新核心(mScore);
计算正确答案++;
mButtonChoice1.挫折背景色(颜色.绿色);
//这行代码是可选的
Toast.makeText(NumberSystem1Activity.this,“正确答案”,Toast.LENGTH_SHORT.show();
}
否则{
//问题后延迟码
最终处理程序句柄=新处理程序();
可运行延迟=新的可运行(){
公开募捐{
//代码以她的结尾
updateQuestion();
}
};
处理延迟(延迟,2000年);
mScore=(mScore-1);
计算错误的答案++;
更新核心(mScore);
mButtonChoice1.背景色(颜色:红色);
//这行代码是可选的
Toast.makeText(NumberSystem1Activity.this,“错误答案”,Toast.LENGTH_SHORT.show();
}
}else if(mbutonchoice2.isChecked()){
if(mButtonChoice2.getText()==mAnswer){
//问题后延迟码
最终处理程序句柄=新处理程序();
可运行延迟=新的可运行(){
公开募捐{
//代码以她的结尾
updateQuestion();
}
};
处理延迟(延迟,2000年);
mScore=mScore+3;
更新核心(mScore);
计算正确答案++;
mButtonChoice2.背景色(颜色.绿色);
//这行代码是可选的
Toast.makeText(NumberSystem1Activity.this,“正确答案”,Toast.LENGTH_SHORT.show();
}
否则{
//问题后延迟码
最终处理程序句柄=新处理程序();
可运行延迟=新的可运行(){
公开募捐{
//代码以她的结尾
updateQuestion();
}
};
处理延迟(延迟,2000年);
mScore=(mScore-1);
计算错误的答案++;
更新核心(mScore);
mButtonChoice2.背景色(颜色:红色);
//这行代码是可选的
Toast.makeText(NumberSystem1Activity.this,“错误答案”,Toast.LENGTH_SHORT.show();
}
}e