Sending radio button data to next activity

后端 未结 2 1276
臣服心动
臣服心动 2021-01-19 08:10

I\'m trying to allow the user to enter their Name, and click on one of three radio buttons, and click on a submit button. And on the next activity, it will display their nam

相关标签:
2条回答
  • 2021-01-19 08:39

    - Use switch to get the Radio button selected.

    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 the startActivity(intent);

    0 讨论(0)
  • 2021-01-19 08:41
    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 recieve the data in the next activity, use

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String message= extras.getString("radioChosen");
    
    }
    
    0 讨论(0)
提交回复
热议问题