1

我正在尝试进行测验,用户在每个活动中选择一个答案,最后一页根据所选选项提供答案。因此,我想将这些值传递给最终活动,但似乎只显示最后选择的值。

第一个活动:

public class Quiz extends Activity {

Button btn;
RadioGroup rg;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz);

    btn = (Button) findViewById(R.id.nextBtn);
    rg= (RadioGroup) findViewById(R.id.rg);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
            Intent intent = new Intent(getApplicationContext(), Quiz1.class);
            Bundle bundle = new Bundle();
            int id = rg.getCheckedRadioButtonId();
            RadioButton radioButton = (RadioButton) findViewById(id);
            bundle.putString("rg", radioButton.getText().toString());
            intent.putExtras(bundle);
            startActivity(intent);

        }

        }

    });
  }
}

第二个活动:

public class Quiz1 extends Activity {

Button btn;
RadioGroup rg1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz1);

    btn = (Button) findViewById(R.id.nextBtn1);
    rg1= (RadioGroup) findViewById(R.id.rg1);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg1.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
                Intent intent1 = new Intent(getApplicationContext(), Quiz2.class);
                Bundle bundle1 = new Bundle();
                int id = rg1.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle1.putString("rg1", radioButton.getText().toString());
                intent1.putExtras(bundle1);
                startActivity(intent1);

            }

        }

    });
  }
}

现在总共有 7 个活动。不包括最终活动这是第 7 个(称为 Quiz6)活动:

public class Quiz6 extends Activity {

Button btn;
RadioGroup rg6;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz6);


    btn = (Button) findViewById(R.id.nextBtn6);
    rg6= (RadioGroup) findViewById(R.id.rg6);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg6.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
                Intent intent6 = new Intent(getApplicationContext(), Final1.class);
                Bundle bundle6 = new Bundle();
                int id = rg6.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle6.putString("rg6", radioButton.getText().toString());
                intent6.putExtras(bundle6);
                startActivity(intent6);

            }

        }

    });
  }
}

你明白了:)

这是最终活动(称为 Final1),这里显示结果

这是代码

public class Final1 extends Activity {

Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.final1);

    Bundle bundle = getIntent().getExtras();

    TextView textView = (TextView)findViewById(R.id.txt);
    textView.setText(bundle.getCharSequence("rg"));

    Bundle bundle1 = getIntent().getExtras();
    TextView textView1 = (TextView)findViewById(R.id.txt1);
    textView.setText(bundle1.getCharSequence("rg1"));

    Bundle bundle2 = getIntent().getExtras();
    TextView textView2 = (TextView)findViewById(R.id.txt2);
    textView.setText(bundle2.getCharSequence("rg2"));

    Bundle bundle3 = getIntent().getExtras();
    TextView textView3 = (TextView)findViewById(R.id.txt3);
    textView.setText(bundle3.getCharSequence("rg3"));

    Bundle bundle4 = getIntent().getExtras();
    TextView textView4 = (TextView)findViewById(R.id.txt4);
    textView.setText(bundle4.getCharSequence("rg4"));

    Bundle bundle5 = getIntent().getExtras();
    TextView textView5 = (TextView)findViewById(R.id.txt5);
    textView.setText(bundle5.getCharSequence("rg5"));

    Bundle bundle6 = getIntent().getExtras();
    TextView textView6 = (TextView)findViewById(R.id.txt6);
    textView.setText(bundle6.getCharSequence("rg6"));

    btn = (Button)findViewById(R.id.restartBtn);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(v.getContext(), Quiz.class);
            startActivityForResult(in, 0);
        }
    });

  }

}

一旦我运行程序,最终发生的事情是只有“textView”最终会在最终活动之前更改为活动上的所选选项,如上所示

任何帮助表示赞赏,非常感谢<3

4

1 回答 1

0

一个活动的意图只有一个Bundle与之关联。因此,您只是将最后一个包传递给您的最终活动,而其他包则传递给紧随其后的活动。您可以通过以下方式解决此问题

Bundle bundle = getIntent().getExtras(); //this gets the current activity's extras
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg1", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);

而不是像现在这样每次都创建一个新包。然后将此捆绑包传递给以下活动。这样,每次获得新答案时,您都将其添加到您的答案包中,而不是每次都创建一个只有一个项目的新包。

然后在你的最后一个活动中,你只需要得到一个包,并且可以从中提取所有答案。注意:您需要确保使用正确的文本更新正确的文本视图。在您的问题中,textView是唯一更新的视图,6 次,而其他视图根本没有更新。

Bundle bundle = getIntent().getExtras();

TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));

TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));

TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));

TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));

TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));

TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));

TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
于 2016-01-07T00:35:57.583 回答