我需要恢复用户选择的选项并将其传递给另一个活动,我该怎么做?如何从另一个活动中的RadioGroup中恢复数据?
public class SelectServicesActivity extends AppCompatActivity {
Button btn_seleccion;
RadioGroup radioGroup;
TextView tvServicio1, tvServicio2, tvServicio3;
RadioButton rb1, rb2, rb3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_services);
radioGroup = (RadioGroup) findViewById(R.id.rg_servicios);
btn_seleccion = (Button) findViewById(R.id.btn_seleccion);
rb1 = (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rb3 = (RadioButton) findViewById(R.id.rb3);
btn_seleccion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
// no radio buttons are checked
Toast.makeText(getApplicationContext(), "Por favor, selecciona una opción", Toast.LENGTH_SHORT).show();
}
else
{
// one of the radio buttons is checked
Intent intent = new Intent (v.getContext(), SelectDayActivity.class);
startActivityForResult(intent, 0);
}
}
});
}}
用户选择的选项必须在另一个活动中检索。
谢谢
我认为我需要声明一个字符串变量,你怎么看? –
您不需要在SelectServicesActivity.class上声明一个String变量。但在SelectDayActivity.class上,您可以全局声明字符串selected_option。 –
我编译了我的代码,但在第二个活动中未显示所选项目 –