Home > other >  Get text from multiples radiobuttons
Get text from multiples radiobuttons

Time:11-12

I want to get the text from multiples checked radiobuttons, and retrieve it in one edittext... but dont know how is the best way to do it. What is better? saving results in string or a list?

Anyway the result should appear in one single editext with the space beetwen each text. For example... Dog, Cat, Mouse.

if (radioButton.isChecked()){
                String obs2 = radioButton.getText().toString();
                texto.setText(obs2);
            }
            if (radioButton3.isChecked()){
                String obs2 = radioButton3.getText().toString();
                texto.setText(obs2);
            }
            if (radioButton4.isChecked()){
                String obs2 = radioButton4.getText().toString();
                texto.setText(obs2);
            }
            if (radioButton5.isChecked()){
                String obs2 = radioButton5.getText().toString();
                texto.setText(obs2);
            }
            if (radioButton6.isChecked()){
                String obs2 = radioButton6.getText().toString();
                texto.setText(obs2);
            }
            if (radioButton7.isChecked()){
                String obs2 = radioButton7.getText().toString();
                texto.setText(obs2);
            }
            if (radioButton8.isChecked()){
                String obs2 = radioButton8.getText().toString();
                texto.setText(obs2);
            }

            editText1 = here should get the text of each radio button checked

new try:

if (radioButton.isChecked()){
                obs2 = checkNullAndAppend(obs2, (String) radioButton.getText());
            } else if (radioButton3.isChecked()){
                obs2 = checkNullAndAppend(obs2, (String) radioButton3.getText());
            } else if (radioButton4.isChecked()){
                obs2 = checkNullAndAppend(obs2, (String) radioButton4.getText());
            } else {
                texto.setText(obs2);
            }

CodePudding user response:

If you want event-driven code then using a list may be easier to manage depending on how many buttons you have and how you do it. But in the case of your exact example, a string is more than enough. As suggested in comments you can simply append the text texto.setText(texto.getText() "," obs2);.

If you want to avoid issues with null values or reduce duplicate code, then there are many different ways you can do this, but one is to create a simple method:

public static String checkNullAndAppend(String existing, String toAppend){
    //Check null
    if (existing == null || existing.equals(""))
        return toAppend;
    //Otherwise add a comma and space
    else
        return existing   ", "   toAppend;
}

Then you can simply call the method inside your if statements:

if (radioButton.isChecked()){
    texto = checkNullAndAppend(texto, radioButton3.getText());
}
if (radioButton3.isChecked()){
    texto = checkNullAndAppend(texto, radioButton3.getText());
}
...

editText1 = texto;
...
  • Related