java - 如何以编程方式将文本设置为 RadioGroup 内部的 RadioButton?

标签 java android

我有一个 RadioGroup,其中有一些 RadioButton

我想以编程方式在 RadioButton 上设置一些文本。我使用了以下代码,因为我无法使用 Java 访问 RadioGroup 中的 RadioButton

如何在 RadioGroup 中的 RadioButton 上设置文本?

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

关于java - 如何以编程方式将文本设置为 RadioGroup 内部的 RadioButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6426317/

相关文章:

java - 如何使用 JPA 和 JUnit 对不同的持久性单元进行测试?

java - 使用 Hibernate Envers 审核可嵌入实体

android - onConfigurationChanged 没有被调用

android - 如何自定义 AOSP 内部版本号?

java - "This Activity already has an action bar supplied by the window decor..."错误

java - 使用jdbc在Oracle sql中过滤特定日期范围内的数据

java - spring 中@Value 的插入顺序不明确

java - 单击按钮Android后绘制

来自 PreviewCallback 的 android 相机发布错误

java - 通过使用二维数组创建井字棋盘