如何从另一个活动中的RadioGroup恢复数据?

时间:2017-05-28 07:40:26

标签: android radio-group data-recovery

我需要恢复用户选择的选项并将其传递给其他活动,我该怎么办?

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

            }

             }

    });


     }}

必须在另一项活动中检索用户选择的选项。

谢谢

3 个答案:

答案 0 :(得分:0)

试试这个

Intent intent = new Intent (v.getContext(), SelectDayActivity.class);
intent.putExtra("value", valueToSend);
                startActivityForResult(intent, 0);

然后在第二个活动中获取onCreate()

String value = getIntent().getStringExtra("value");

答案 1 :(得分:0)

我不是100%肯定,但你可以点击按钮

添加以下行
int selectedId = radioGroup .getCheckedRadioButtonId();
if(selectedId == -1) {
    Toast.makeText(getApplicationContext(), "Por favor, selecciona una opción", Toast.LENGTH_SHORT).show();
    return;
}

RadioButton radioButton = (RadioButton) findViewById(selectedId);
String selected_option = radioButton.getText().toString().trim();
Intent intent = new Intent (SelectServicesActivity , SelectDayActivity.class);
Bundle bundle = new Bundle();
bundle.putString("SELECTED_ITEM", selected_option);
intent.putExtra("SELECTED_BUNDLE", bundle);
startActivityForResult(intent, 0);

现在在onCreate()内接收端(SelectDayActivity.class)检索这样的selected_option

Bundle bundle = getIntent().getBundleExtra("SELECTED_BUNDLE");
String value = bundle.getString("SELECTED_ITEM");
Log.e("TAG", "_log : value : " + value); // should print your option

答案 2 :(得分:0)

我修改了这部分并添加了Toast来验证所选的选项:

int selectedId = radioGroup.getCheckedRadioButtonId();
            RadioButton rb = (RadioButton) radioGroup.findViewById(selectedId);

            if(selectedId == -1) {
                Toast.makeText(getApplicationContext(), "Por favor, selecciona una opción", Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(SelectServicesActivity.this, "servicio " + rb.getText().toString(), Toast.LENGTH_LONG).show();

            //RadioButton radioButton = (RadioButton) findViewById(selectedId);
            String selected_option = rb.getText().toString().trim();
            Intent intent = new Intent (SelectServicesActivity.this, SelectDayActivity.class);
            intent.putExtra("SELECTED_ITEM", selected_option);
            startActivityForResult(intent, 0);


             }

在我希望收到相关信息的其他活动中,我补充说:

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

    textView_tipo_servicio = (TextView) findViewById(R.id.textView_tipo_servicio);
    String selected_option = getIntent().getStringExtra("SELECTED_ITEM");
    textView_tipo_servicio.setText(selected_option);

问题是没有显示信息。