Java 如何以编程方式将文本设置为RadioButton(位于放射组内部)?
我有一个Java 如何以编程方式将文本设置为RadioButton(位于放射组内部)?,java,android,Java,Android,我有一个RadioGroup,里面有一些RadioButtons 我想以编程方式在单选按钮上设置一些文本。我使用了以下代码,因为我无法使用Java访问RadioGroup中的RadioButtons 如何设置放射组内单选按钮上的文本 XML布局: <RadioGroup android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_height="wrap_conten
RadioGroup
,里面有一些RadioButton
s
我想以编程方式在单选按钮上设置一些文本。我使用了以下代码,因为我无法使用Java访问RadioGroup
中的RadioButton
s
如何设置放射组内单选按钮上的文本
XML布局:
<RadioGroup android:layout_width="fill_parent"
android:layout_marginLeft="20dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/QueGroup1">
<RadioButton android:checked="false"
android:button="@drawable/green"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textColor="#000"
android:text="Yes"
android:id="@+id/rbtnYes"
android:visibility="gone" />
<RadioButton android:checked="false"
android:button="@drawable/red"
android:textColor="#000"
android:text="No"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/rbtnNo"
android:visibility="gone" />
<RadioButton android:checked="false"
android:button="@drawable/red"
android:textColor="#000"
android:text="Dont Know"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/rbtnDontKnow"
android:visibility="gone" />
</RadioGroup>
Java代码:
private void fnRadioTextSet(String strval) {
rbtnYes = (RadioButton)findViewById(R.id.rbtnYes);
rbtnNo = (RadioButton)findViewById(R.id.rbtnNo);
rbtnDontKnow = (RadioButton)findViewById(R.id.rbtnDontKnow);
RadioGroup rbtnGrp = (RadioGroup)findViewById(R.id.QueGroup1);
String[] strArrtext = strval.split(",");
for (int intcount = 0; intcount < strArrtext.length; intcount++) {
rbtnGrp.getChildAt(intcount).settext("test");
}
//int i = rbtnGrp.getChildCount();
//Toast.makeText(getApplication(), rbtnGrp.getChildCount(),
// Toast.LENGTH_LONG).show();
/*String[] strtext = strval.split(",");
if (strtext.length > 0) {
}*/
}
private void fnRadioTextSet(字符串strval){
rbtnYes=(单选按钮)findViewById(R.id.rbtnYes);
rbtnNo=(单选按钮)findViewById(R.id.rbtnNo);
rbtnDontKnow=(单选按钮)findViewById(R.id.rbtnDontKnow);
放射组rbtnGrp=(放射组)findViewById(R.id.QueGroup1);
字符串[]strArrtext=strval.split(“,”);
对于(int-int-count=0;int-count0){
}*/
}
不需要循环
rbtnYes.settext("sdklfhjsdf");
rbtnNo.settext("test");
rbtnDontKnow.settext("test");
private void fnRadioTextSet(字符串strval){
放射组rbtnGrp=(放射组)findViewById(R.id.QueGroup1);
字符串[]strArrtext=strval.split(“,”);
对于(int i=0;i
谢谢您的回答,但我想使用radio group object设置单选按钮文本值。可以这样做吗?您好!我也这么做了,但是文本在单选按钮下滑动,我看不到它的存在。添加填充或可拉伸填充对我没有帮助。有什么想法吗?谢谢
private void fnRadioTextSet(String strval) {
RadioGroup rbtnGrp = (RadioGroup)findViewById(R.id.QueGroup1);
String[] strArrtext = strval.split(",");
for (int i = 0; i < rbtnGrp.getChildCount(); i++) {
((RadioButton) rbtnGrp.getChildAt(i)).setText(strArrtext[i]);
}
}