Sending switch data to next activity

I am trying to allow the user to enter their name and click on one of the three radio buttons and press the submit button. And in the next action, he will display his name and the switch that they selected. I managed to send the name, but I'm not sure how to send the select switch. Can anyone help?

This is what I have in the main .xml activity layout

<EditText android:id="@+id/operatorName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:ems="10" android:hint="@string/operator_name" /> <RadioGroup android:id="@+id/radioShifts" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="10dp" > <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radio1" android:checked="true" android:onClick="onRadioButtonClicked" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radio2" android:onClick="onRadioButtonClicked" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radio3" android:onClick="onRadioButtonClicked" /> </RadioGroup> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="28dp" android:text="@string/button1" android:onClick="onButton1" /> 

And I have this in the main_activity.java file:

  public final static String OP_NAME = "com.cyapps.downtimer.OPNAME"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } 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) break; case R.id.radioButton2: if (checked) break; case R.id.radioButton3: if (checked) break; } /* Intent intent = new Intent(this, WinderDTActivity.class); EditText button = (EditText) findViewById(RadioGroup.getCheckedRadioButtonId()); String radioChosen = button.getText().toString(); intent.putExtra(RADIO_CHOSEN, radioChosen);*/ } public void onButton1(View view) { Intent intent = new Intent(this, WinderDTActivity.class); EditText editText = (EditText) findViewById(R.id.operatorName); String opName = editText.getText().toString(); intent.putExtra(OP_NAME, opName); startActivity(intent); } 

The code in / * * / is what I think I should do. But I'm not sure. Anyone help please? I will be very grateful for that.

+4
source share
2 answers
 String str; // store the text corresponding to the RadioButton which is clicked switch(view.getId()) { case R.id.radioButton1: if (checked) str = "button1Text"; break; case R.id.radioButton2: if (checked) str = "button2Text"; break; case R.id.radioButton3: if (checked) str = "button3Text"; break; } Intent intent = new Intent(this, WinderDTActivity.class); intent.putExtra("radioChosen", str); // pass "str" to the next Activity 

EDIT: To get the data in the next step, use

 Bundle extras = getIntent().getExtras(); if (extras != null) { String message= extras.getString("radioChosen"); } 
+3
source

- Use switch to select the Radio button.

 switch (types.getCheckedRadioButtonId()) { case R.id.sit_down: intent.putExtra(SIT_DOWN, "sitdown"); break; case R.id.take_out: intent.putExtra(TAKE_OUT, "takeout"); break; } 

- Now just do startActivity(intent);

0
source

Source: https://habr.com/ru/post/1439431/


All Articles