将通过无线电组做出的选择转移到另一个意图

时间:2019-06-23 21:39:41

标签: java android android-intent

我在Main3Activity中有4个无线电组(例如几个是或否),我希望将这些选择发送到另一个意图(Main4Activity)。我将button12用于更改意图。

我尝试执行此代码

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;
    }
}

}

在接下来的意图中,我尝试使用与此类似的方法来显示结果:

 String snoring = getIntent().getExtras().getString("snoring");
 String pressure= getIntent().getExtras().getString("pressure");

但“打nor”和“压力”始终为0。 如何从以前的意图中恢复打s和压力数据?

1 个答案:

答案 0 :(得分:0)

    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
}