GenEdQuestion.class 测验模式是多种选择。我想保存或将正确答案和所选答案转移到另一个活动(结果)。我转移了分数,但我不知道如何转移结果。我怎么能这样做?
answer1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (answer1.getText() == mAnswer){
mNumber++;
mScore++;
number.setText(" "+mNumber+" .");
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}else{
mNumber++;
number.setText(" "+mNumber+" .");
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}
return false;
}
});
answer2.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (answer2.getText() == mAnswer){
mNumber++;
number.setText(" "+mNumber+" .");
mScore++;
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}else{
mNumber++;
number.setText(" "+mNumber+" .");
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}
return false;
}
});
answer3.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (answer3.getText() == mAnswer){
mNumber++;
number.setText(" "+mNumber+" .");
mScore++;
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}else{
mNumber++;
number.setText(" "+mNumber+" .");
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}
return false;
}
});
answer4.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (answer4.getText() == mAnswer){
mNumber++;
number.setText(" "+mNumber+" .");
mScore++;
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}else{
mNumber++;
number.setText(" "+mNumber+" .");
if (mNumber == quizCount){
onFinish();
}
else{
updateQuestion(r.nextInt(mQuestionsLength));
}
}
return false;
}
});
new CountDownTimer(60000*60, 1000){
public void onTick(long secondsLeft){
mCountDownGenEd.setText(""+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(secondsLeft),
TimeUnit.MILLISECONDS.toSeconds(secondsLeft) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(secondsLeft))));
}
public void onFinish(){
Intent intent = new Intent(getApplicationContext(), GenEdQuestionsResult.class);
intent.putExtra("this", mScore);
startActivity(intent);
}
}.start();
}
private void onFinish() {
Intent intent = new Intent(getApplicationContext(), GenEdQuestionsResult.class);
intent.putExtra("this", mScore);
startActivity(intent);
}
private void updateQuestion(int i) {
question.setText(mQuestions.getQuestion(i));
answer1.setText(mQuestions.getChoice1(i));
answer2.setText(mQuestions.getChoice2(i));
answer3.setText(mQuestions.getChoice3(i));
answer4.setText(mQuestions.getChoice4(i));
mAnswer = mQuestions.getCorrectAnswer(i);
}
GenEdQuestionsResult.class 此活动将显示分数以及用户是否通过或失败。我如何将结果放在这里?
mFinalScore = (TextView) findViewById(R.id.scoreTextView);
mResult = (TextView) findViewById(R.id.resultTextView);
int score = getIntent().getIntExtra("this", 0);
mFinalScore.setText(score+" / 20");
if (score <= 10){mResult.setText("Failed");}
else{mResult.setText("Passed");}
这是我想要创建的结果
答案 0 :(得分:2)
三种方式,共享首选项或getter和setter方法,或者在开始下面的新活动时使用putextra:
共享偏好
在首选项中设置值:
// MY_PREFS_NAME - a static String variable like:
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.apply();
从偏好中检索数据:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
}
参考:https://stackoverflow.com/a/23024962/4674760
Getter / Setter参与新活动
String newenq="New Enquiry";
public void setNewenq(String newenq) {
this.newenq = newenq;
}
public String getNewenq() {
return newenq;
}
PutExtra
String easyPuzzle = "answer";
Intent i = new Intent(this, ToClass.class);
i.putExtra("epuzzle", easyPuzzle);
startActivity(i)
答案 1 :(得分:1)
你有三种方法可以做到这一点 第一个=&gt;创建一个静态变量,然后您可以随意访问它 ,
第二个你可以=&gt;使用 intent putExtra
Intent i = new Intent(this, SecondClass.class);
i.putExtra("answer", "myAnswer");
startActivity(i)
第三种方法 正在使用sharedPreference来查看此链接https://developer.android.com/reference/android/content/SharedPreferences.html
答案 2 :(得分:0)
使用像这样的共享偏好:
public static final String answer = "answer"; //declare this globally at the top.
SharedPreferences prefs = getSharedPreferences(Commonclass.ANSWER,Context.MODE_PRIVATE);
SharedPreferences.Editor editor;
editor = prefs.edit();
String a = prefs.getString("answer","answer"); //to get the stored value
editor.putString(theme,"blue"); // to store your answer
editor.commit();
创建一个公共类:
public class Commonclass
{
public static final String ANSWER = "answer" ;
}