简体   繁体   English

从多个单选按钮获取文本

[英]Get text from multiples radiobuttons

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

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);正如评论中所建议的,您可以简单地附加文本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语句中调用该方法:

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

editText1 = texto;
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM