2014-10-28 215 views
0

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

这是我的单选按钮

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孩子

但上运行的应用程序中的文本是不可见的代码。 任何人都可以检测到问题。 非常感谢

+0

尝试设置文本,然后使用'bg.addView(radioButton);'也可以通过setId() ' – 2014-10-28 07:31:29

回答

2

只需两个单选按钮添加

setLayoutParams(params); 

以及RadioGroup中。

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

发生这种情况的时间很多,因为没有足够的空间分配给小部件,并且它们无法显示文本,尽管它在那里。如果调试器显示文本,那么代码很好......检查XML并为小部件指定适当的空间。尝试分配编码的文本来澄清。

+0

非常感谢您的回答。但我没有xml我正在生成java.l的整个布局,我可以分配适当的空间,因为我使用的是布局布局,而不是给出任何空间。 – 2014-10-28 07:36:22

+0

您可以发布在应用程序的什么 – vkm 2014-10-28 07:39:45

+1

屏幕截图 – 2014-10-28 07:40:09

2

只要改变文字颜色和显示文本。 默认情况下,文本颜色是白色的,n这是我的情况

+0

可能是一个,可能是。但不是在我的情况。但谢谢你的回答:) – 2015-08-01 12:08:16