-1

我正在尝试创建一个问答游戏,该游戏将在活动结束时显示用户在活动中的得分。我有 50 个问题,但每次用户玩游戏时只会显示 10 个问题。我遇到了麻烦,因为 a 无法保存用户的分数。

以下是我的java代码,假设Q1.java并且Q2.java是我的java类。Whoami.java是用户开始测验的代码,score.java也是显示分数的地方。请帮帮我!提前致谢!

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question1);

    TextView q1 = (TextView) findViewById(R.id.q1);
    q1.setText("I am an amphibian who has undergone metamorphosis. I have slippery skin, and spend most of my time in or near water. I breathe with lungs now, but when I was a tadpole I had gills.");
}

@Override
public void onClick(View arg0) 
{
    // TODO Auto-generated method stub
    int cnt = 0;

    final Animation animScale = AnimationUtils.loadAnimation(this, R.animator.anim_scale);
    final Animation animAlpha = AnimationUtils.loadAnimation(this, R.animator.anim_alphas);

    Button btn1 = (Button) findViewById(R.id.btnopt1_a);
    Button btn2 = (Button) findViewById(R.id.btnopt1_b);
    Button btn3 = (Button) findViewById(R.id.btnopt1_c);

    ImageView img1 = (ImageView) findViewById(R.id.imageView1);
    ImageView img2 = (ImageView) findViewById(R.id.imageView2);
    ImageView img3 = (ImageView) findViewById(R.id.imageView3);

    img1.setOnClickListener(new ImageView.OnClickListener()
    {
            @Override
            public void onClick(View arg0) {
                arg0.startAnimation(animScale);
            }});

    img2.setOnClickListener(new ImageView.OnClickListener()
    {
            @Override
            public void onClick(View arg0) {
                arg0.startAnimation(animAlpha);
            }});

    img3.setOnClickListener(new ImageView.OnClickListener()
    {
            @Override
            public void onClick(View arg0) {
                arg0.startAnimation(animAlpha);
            }});

    if(btn1.isClickable())      
    {
        img1.startAnimation(animScale);
        cnt++;  
        Intent i = new Intent(this,Q2.class);
        i.putExtra("score",cnt);
        startActivity(i);
    }

    else if (btn2.isClickable())
    {
        img2.startAnimation(animAlpha);
    }

    else if (btn3.isClickable())
    {
        img3.startAnimation(animAlpha);
    }
}
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }
}

那么这是我的另一个类名 Q2.java

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question2);

    TextView q2 = (TextView) findViewById(R.id.q2);
    q2.setText("I am a bird that has wings, but does not fly. I spend most of my time in the water. I am known to live in colder regions like Antarctica.");
}

@Override
public void onClick(View arg0) 
{
    // TODO Auto-generated method stub
    int cnt = 0;

    final Animation animScale = AnimationUtils.loadAnimation(this, R.animator.anim_scale);
    final Animation animAlpha = AnimationUtils.loadAnimation(this, R.animator.anim_alphas);

    Button btn1 = (Button) findViewById(R.id.btnopt2_a);
    Button btn2 = (Button) findViewById(R.id.btnopt2_b);
    Button btn3 = (Button) findViewById(R.id.btnopt2_c);

    ImageView img1 = (ImageView) findViewById(R.id.imageView1);
    ImageView img2 = (ImageView) findViewById(R.id.imageView2);
    ImageView img3 = (ImageView) findViewById(R.id.imageView3);

    img1.setOnClickListener(new ImageView.OnClickListener()
    {
            @Override
            public void onClick(View arg0) {
                arg0.startAnimation(animScale);
            }});

    img2.setOnClickListener(new ImageView.OnClickListener()
    {
            @Override
            public void onClick(View arg0) {
                arg0.startAnimation(animAlpha);
            }});

    img3.setOnClickListener(new ImageView.OnClickListener()
    {
            @Override
            public void onClick(View arg0) {
                arg0.startAnimation(animAlpha);
            }});

    if(btn1.isClickable())      
    {
        img1.startAnimation(animScale);
        cnt++;  
        Intent i = new Intent(this,Q3.class);
        i.putExtra("score",cnt);
        startActivity(i);
    }

    else if (btn2.isClickable())
    {
        img2.startAnimation(animAlpha);
    }

    else if (btn3.isClickable())
    {
        img3.startAnimation(animAlpha);
    }
}
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }
}

这是我的 Score.java

 @Override
    public void onCreate(Bundle savedInstanceState) 
 {
     try
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);

        Intent intent = getIntent();
        int score = intent.getIntExtra("score",0);

        Button btnok = (Button)findViewById(R.id.btnok);
        btnok.setOnClickListener(this);

        TextView txtscore = (TextView) findViewById(R.id.txtscore);
        txtscore.setText("Your score is : " + score +"/10");
     }
     catch(Exception e)
     {
         try {
            throw e;
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
     }
 }

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) 
    {

case R.id.btnok:
     Intent i = new Intent (this,MainMenu.class);
     startActivity(i);       
default:
     break;
    }

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

最后是我的 Whoami.java 类

public class Whoami extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_whoami);


 Button bplay =(Button) findViewById(R.id.bplay);
 bplay.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Random random = new Random(); 

        switch(random.nextInt(50)) 
        { 

           case 0:
            Intent newActivity1 = new Intent(Whoami.this,Q1.class);     
            startActivity (newActivity1);
           break;
           case 1:
            Intent newActivity2 = new Intent(Whoami.this,Q2.class);     
            startActivity(newActivity2);
           break;
           case 2:
            Intent newActivity3 = new Intent(Whoami.this,Q3.class);     
            startActivity(newActivity3);
           break;
           case 3:
            Intent newActivity4 = new Intent(Whoami.this,Q4.class);     
            startActivity(newActivity4);
           break;
           case 4:
            Intent newActivity5 = new Intent(Whoami.this,Q5.class);     
            startActivity(newActivity5);
            break;
           case 5:
            Intent newActivity6 = new Intent(Whoami.this,Q6.class);     
            startActivity(newActivity6);
            break;
           case 6:
            Intent newActivity7 = new Intent(Whoami.this,Q7.class);     
            startActivity(newActivity7);
            break;
           case 7:
            Intent newActivity8 = new Intent(Whoami.this,Q8.class);     
            startActivity(newActivity8);
            break;
           case 8:
            Intent newActivity9 = new Intent(Whoami.this,Q9.class);     
            startActivity(newActivity9);
            break;
           case 9:
            Intent newActivity10 = new Intent(Whoami.this,Q10.class);     
            startActivity(newActivity10);
            break;
           case 10:
             Intent newActivity11 = new Intent(Whoami.this,Q11.class);     
             startActivity(newActivity11);
             break;
           case 11:
             Intent newActivity12 = new Intent(Whoami.this,Q12.class);     
             startActivity(newActivity12);
             break;
           case 12:
             Intent newActivity13 = new Intent(Whoami.this,Q13.class);     
             startActivity(newActivity13);
             break;
           case 13:
               Intent newActivity14 = new Intent(Whoami.this,Q14.class);
               startActivity(newActivity14);
               break;
           case 14:
               Intent newActivity15 = new Intent(Whoami.this,Q15.class);
               startActivity(newActivity15);
               break;
           case 15:
               Intent newActivity16 = new Intent(Whoami.this,Q16.class);
               startActivity(newActivity16);
               break;
           case 16:
               Intent newActivity17 = new Intent(Whoami.this,Q17.class);     
               startActivity (newActivity17);
               break;
           case 17:
               Intent newActivity18 = new Intent(Whoami.this,Q18.class);     
               startActivity(newActivity18);
               break;
           case 18:
               Intent newActivity19 = new Intent(Whoami.this,Q19.class);     
               startActivity(newActivity19);
               break;
           case 19:
               Intent newActivity20 = new Intent(Whoami.this,Q20.class);     
               startActivity(newActivity20);
               break;
           case 20:
               Intent newActivity21 = new Intent(Whoami.this,Q21.class);     
               startActivity(newActivity21);
               break;
           case 21:
               Intent newActivity22 = new Intent(Whoami.this,Q22.class);     
               startActivity(newActivity22);
               break;
           case 22:
               Intent newActivity23 = new Intent(Whoami.this,Q23.class);     
               startActivity(newActivity23);
               break;
           case 23:
               Intent newActivity24 = new Intent(Whoami.this,Q24.class);     
               startActivity(newActivity24);
               break;
           case 24:
               Intent newActivity25 = new Intent(Whoami.this,Q25.class);     
               startActivity(newActivity25);
               break;
           case 25:
               Intent newActivity26 = new Intent(Whoami.this,Q26.class);     
               startActivity(newActivity26);
               break;
           case 26:
                Intent newActivity27 = new Intent(Whoami.this,Q27.class);     
                startActivity(newActivity27);
                break;
           case 27:
                Intent newActivity28 = new Intent(Whoami.this,Q28.class);     
                startActivity(newActivity28);
                break;
           case 28:
                Intent newActivity29 = new Intent(Whoami.this,Q29.class);     
                startActivity(newActivity29);
                break;
           case 29:
                  Intent newActivity30 = new Intent(Whoami.this,Q30.class);
                  startActivity(newActivity30);
                  break;
           case 30:
                  Intent newActivity31 = new Intent(Whoami.this,Q31.class);
                  startActivity(newActivity31);
                  break;
           case 31:
                  Intent newActivity32 = new Intent(Whoami.this,Q32.class);
                  startActivity(newActivity32);
                  break;
           case 32:
                  Intent newActivity33 = new Intent(Whoami.this,Q33.class);     
                  startActivity(newActivity33);
                  break;
           case 33:
                  Intent newActivity34 = new Intent(Whoami.this,Q34.class);     
                  startActivity(newActivity34);
                  break;
           case 34:
                  Intent newActivity35 = new Intent(Whoami.this,Q35.class);     
                  startActivity(newActivity35);
                  break;
           case 35:
                  Intent newActivity36 = new Intent(Whoami.this,Q36.class);     
                  startActivity(newActivity36);
                  break;
           case 36:
                  Intent newActivity37 = new Intent(Whoami.this,Q37.class);     
                  startActivity(newActivity37);
                  break;
           case 37:
                  Intent newActivity38 = new Intent(Whoami.this,Q38.class);     
                  startActivity(newActivity38);
                  break;
           case 38:
                  Intent newActivity39 = new Intent(Whoami.this,Q39.class);     
                  startActivity(newActivity39);
                  break;
           case 39:
                  Intent newActivity40 = new Intent(Whoami.this,Q40.class);     
                  startActivity(newActivity40);
                  break;
           case 40:
                  Intent newActivity41 = new Intent(Whoami.this,Q41.class);     
                  startActivity(newActivity41);
                  break;
           case 41:
                   Intent newActivity42 = new Intent(Whoami.this,Q42.class);     
                   startActivity(newActivity42);
                   break;
           case 42:
                   Intent newActivity43 = new Intent(Whoami.this,Q43.class);     
                   startActivity(newActivity43);
                   break;
           case 43:
                   Intent newActivity44 = new Intent(Whoami.this,Q44.class);     
                   startActivity(newActivity44);
                   break;
           case 44:
                   Intent newActivity45 = new Intent(Whoami.this,Q45.class);
                   startActivity(newActivity45);
                   break;
           case 45:
                   Intent newActivity46 = new Intent(Whoami.this,Q46.class);
                   startActivity(newActivity46);
                   break;
           case 46:
                   Intent newActivity47 = new Intent(Whoami.this,Q47.class);
                   startActivity(newActivity47);
                   break;
           case 47:
                   Intent newActivity48 = new Intent(Whoami.this,Q48.class);     
                   startActivity (newActivity48);
                   break;
           case 48:
                   Intent newActivity49 = new Intent(Whoami.this,Q49.class);     
                   startActivity(newActivity49);
                   break;
           case 49:
                   Intent newActivity50 = new Intent(Whoami.this,Q50.class);     
                   startActivity(newActivity50);
                   break;

        }
    }
    });
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
} 

}

4

1 回答 1

0

随之创建一个 QuestionActivity 类和 xml 布局。将问题作为字符串传递给 Activity 通过

intentInstance.putExtra("question", yourRandomQuestion);

并使用从 QuestionActivity 类中获取传递的值

Intent intent = getIntent();
String question = intent.getStringExtra("question");

然后将问题放入 TextView。

希望能帮助到你:)

于 2013-01-23T10:12:12.710 回答