how can i send radio button value and switch case option to server using json android?

后端 未结 4 1687
忘了有多久
忘了有多久 2021-01-29 10:07

I\'m trying to make a registration form where all values are saving on the server, but i dont know how to send the radio buttons\' value and switch option buttons\' value to ser

相关标签:
4条回答
  • 2021-01-29 10:53

    try this out incase of radio button it works for me.

    radio_button_value1 = ((RadioButton) findViewById(radioGroup1.getCheckedRadioButtonId())).getText().toString();
    

    above code is to get value of the radio button.

    replace your this code

    // get selected radio button from radioGroup
            int selectedId = rgrp.getCheckedRadioButtonId();
    

    with this code and test

    // get selected radio button from radioGroup
     int selectedId = rgrp.getCheckedRadioButtonId().getText().toString();
    

    hope this updated code helps you.you just have to replace and check

    0 讨论(0)
  • 2021-01-29 10:57

    i gt my answer by myself just need to use that code inside button:

     public void onClick(View v) {
    
                RadioGroup rgrp=(RadioGroup)findViewById(R.id.rg);
    
                RadioButton radioButton;
    
                int selectedId = rgrp.getCheckedRadioButtonId();
    
                // find the radiobutton by returned id
                radioButton = (RadioButton) findViewById(selectedId);
    
                Toast.makeText(RegistrationForm.this,
                        radioButton.getText(), Toast.LENGTH_SHORT).show();
    
    0 讨论(0)
  • 2021-01-29 10:59

    you can get the value of checked radio button from OnCheckedChangedListener

    rgrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // checkedId is the RadioButton selected
                RadioButton rb=(RadioButton)findViewById(checkedId);
                Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    
    0 讨论(0)
  • 2021-01-29 11:03

    Add a global String variable gender and boolean variable switchValue.

    String gender="Male" ;
    boolean switchValue = false ;
    

    Inside RadioGroupListener Method

     public void addListenerOnButton() {
        RadioGroup rgrp=(RadioGroup)findViewById(R.id.rg);
        rgrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(i==R.id.rgm){
                  gender="Male" ; 
                 }else{
                  gender="Female";
             }
        }
        });
        Toast.makeText(RegistrationForm.this,
                radioButton.getText(), Toast.LENGTH_SHORT).show();
    
    }
    

    For Getting the switch state

    switchValue = swth.isChecked();
    
    0 讨论(0)
提交回复
热议问题