2011-06-21 1 views
2

私にはRadioGroupがあります。その内にはRadioButtonがあります。RadioGroupの内部にあるRadioButtonにプログラムでテキストを設定するにはどうすればよいですか?

プログラムでRadioButtonのテキストを設定したいとします。私は、Javaを使用してRadioGroupの中のRadioButtonにアクセスすることができないので、次のコードを使用しました。

RadioButtonのテキストをRadioGroupに設定するにはどうすればよいですか?

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) { 

    }*/ 
} 
+0

おかげであなたの答えのための –

答えて

5
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]); 
    } 
} 
+0

こんにちは! 私は同じことをしましたが、テキストはラジオボタンの下にスライドしていて、私はその存在を見ることができません。パディングやdrawablePaddingを追加することは私にとっては役に立ちません。何か案が?どうも – Karoly

1

がループに位置合わせのための

rbtnYes.settext("sdklfhjsdf"); 
rbtnNo.settext("test"); 
rbtnDontKnow.settext("test"); 
+0

おかげで、私を必要としないでください。ラジオグループを使ってラジオボタンのテキスト値を設定したいオブジェクトはそれを行うことができますか? –

関連する問題