【问题标题】:RadioButton.setText() is not displaying the textRadioButton.setText() 不显示文本
【发布时间】:2014-12-23 12:43:11
【问题描述】:

我正在创建 RadioButtons 并将它们动态添加到 RadioGroup。 但是当我运行应用程序时,RadioButtons 的文本没有显示在屏幕上。

这是我的 RadioButtons 代码

else if ((items.get(i).toString()).equals("rad")) {
                RadioGroup bg = new RadioGroup(getApplicationContext());
                int child=0;
                for (int h = textlen; h < text.size(); textlen++) {

                    if (text.get(textlen).contains("(")) {

                        s = text.get(textlen).replace("(", "");

                        if (s.contains(")"))
                            s = s.replace(")", "");

                    } else if (text.get(textlen).contains(")")) {
                        s = text.get(textlen).replace(")", "");
                    } else
                        s = text.get(textlen);

                    RadioButton radioButton = new RadioButton(
                            getApplicationContext());
                    bg.addView(radioButton);
                    // / radioButton.setName("rbt");
                    if (s.contains("{on}")) {
                    //  radioButton.setSelected(true);
                        radioButton.setChecked(true);
                        s = s.replace("{on}", "");
                    } else {
                        radioButton.setChecked(false);
                        s = s.replace("{of}", "");
                    }
                    //((RadioButton)bg.getChildAt(child)).setText(s);
                    //child++;

                    radioButton.setText(s);

                    String c = text.get(textlen).substring(
                            text.get(textlen).length() - 1);
                    if (c.equals(")")) {
                        textlen++;
                        break;
                    }



                }

                layout.addView(bg);
            }

当我调试代码时,我可以看到文本已添加到 RadioGropus 子项中

但在运行应用程序时,文本是不可见的。 任何人都可以发现这个问题。 非常感谢

【问题讨论】:

  • 尝试先设置文本然后使用bg.addView(radioButton); 也可以使用setId()为单选按钮分配一个id

标签: android radio-button android-radiogroup


【解决方案1】:

只需添加

setLayoutParams(params);

对于 RadioButton 和 RadioGroup。

在哪里

android.widget.LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

【讨论】:

    【解决方案2】:

    只需更改文本颜色,您的文本就会出现。 默认情况下,文本颜色为白色,这就是我的问题

    【讨论】:

    • 可能是一个,可能是。但不是在我的情况下。但是感谢您的回答:)
    【解决方案3】:

    这种情况经常发生,因为没有为小部件分配足够的空间,并且尽管存在文本,但它们无法显示文本。如果调试器显示文本,则代码很好...检查 XML 并为小部件分配适当的空间。尝试分配硬编码文本以澄清。

    【讨论】:

    • 非常感谢您的回答。但是我没有 xml 我在 java 中生成整个布局。我如何分配适当的空间,因为我正在使用 linerlayout 并且不给任何空间。
    • 可以发截图吗
    • 应用中的单选按钮
    • 如你所见,有单选按钮但没有文字:(
    • 尝试在创建单选按钮时动态设置高度和宽度
    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 2019-03-27
    • 2017-04-24
    • 2019-02-05
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    相关资源
    最近更新 更多