我在 Main3Activity 中有 4 个单选组(比如几个是或否),我希望将这些选择发送到另一个 Intent (Main4Activity)。我使用 Button12 来更改 Intent 。
我尝试执行此代码
public class Main3Activity extends AppCompatActivity {
int snoring = 0;
int pressure = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Button btnNew = (Button) findViewById(R.id.button12);
btnNew.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(Main3Activity.this, Main4Activity.class);
if(snoring == 1) {intent.putExtra("snoring", "1");} else {intent.putExtra("snoring", "0");}
if(pressure == 1) {intent.putExtra("pressure", "1");} else {intent.putExtra("pressure", "0");}
startActivity(intent);
}
});
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radioButton1:
if (checked)
snoring = 1;
break;
case R.id.radioButton2:
if (checked)
snoring = 0;
break;
case R.id.radioButton3:
if (checked)
pressure = 1;
break;
case R.id.radioButton4:
if (checked)
pressure = 0;
break;
}
}
}
在下一个 Intent 中,我尝试使用与此类似的方法显示结果:
String snoring = getIntent().getExtras().getString("snoring");
String pressure= getIntent().getExtras().getString("pressure");
但是“打鼾”和“压力”始终为0。 如何从之前的 Intent 中恢复打鼾和压力数据?
最佳答案
Just do-:
btnNew.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(Main3Activity.this, Main4Activity.class);
intent.putExtra("snoring",snoring); //value that came from radio button
intent.putExtra("pressure",pressure) //value that came from radio button
startActivity(intent);
}
});
and on other activity-:
if(intent.hasEXtra("snoring))
{
get the snoring value
}
else if(intent.hasEXtra("pressure))
{
get the pressure value
}
关于java - 将使用 radiogroup 做出的选择转移到另一个 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56727932/