我正在尝试进行一个测验,用户在每个 Activity 中选择一个答案,最终页面根据所选选项提供答案。因此,我想将这些值传递给最终 Activity ,但似乎只显示最后选择的值。
第一个 Activity :
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);
}
}
});
}
}
第二个 Activity :
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 项 Activity ..不包括最后一项 Activity 这是第七个(称为 Quiz6) Activity :
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);
}
}
});
}
}
你明白了:)
这里是最终 Activity (称为 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”最终会更改为最终 Activity 之前的 Activity 上的所选选项,如上所示
感谢任何帮助,非常感谢<3
最佳答案
一项 Activity 的意图只有一个与其关联的Bundle
。因此,您仅将最后一个 bundle 传递给最终 Activity ,其他 bundle 将传递给紧随其后的 Activity 。您可以通过执行以下操作来解决此问题
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);
而不是像现在一样每次都创建一个新的 bundle 。然后将此 bundle 传递给以下 Activity 。这样,每次获得新答案时,您都会将其添加到答案包中,而不是每次创建仅包含一个项目的新包。
然后在你的最终 Activity 中,你只需要得到一个 bundle ,就可以从中找出所有答案。注意:您需要确保使用正确的文本更新正确的 TextView 。在您的问题中,textView
是唯一更新了 6 次的 View ,其他 View 根本没有更新。
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"));
关于java - Android Studio : Passing multiple values using intent PROVIDES ONLY 1 VALUE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34644942/