2017-05-28 62 views
0

我需要恢復用戶選擇的選項並將其傳遞給另一個活動,我該怎麼做?如何從另一個活動中的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); 

      } 

      } 

    }); 


    }} 

用戶選擇的選項必須在另一個活動中檢索。

謝謝

回答

0

試試這個

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

然後在第二個活動得到它onCreate()

String value = getIntent().getStringExtra("value"); 
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); 

現在起接收端(SelectDayActivity.class)內的onCreate()檢索這樣

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

我認爲我需要聲明一個字符串變量,你怎麼看? –

+0

您不需要在SelectServicesActivity.class上聲明一個String變量。但在SelectDayActivity.class上,您可以全局聲明字符串selected_option。 –

+0

我編譯了我的代碼,但在第二個活動中未顯示所選項目 –

0

我修改這個部分,並增加了吐司來驗證所選選項的selected_option:

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

問題是,信息不顯示。

+0

@ msh.nayan在SelectServicesActivity.class上查找 –

+0

Toast顯示正確的選項? –

+0

是的,吐司顯示正確的選項 –

相關問題