如何设置文本单选按钮这是一个RadioGroup中编程里面? [英] How to set text to RadioButton which is inside of a RadioGroup programmatically?

查看:114
本文介绍了如何设置文本单选按钮这是一个RadioGroup中编程里面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RadioGroup中这里面我有一些单选秒。

我想以编程方式设置在单选取值一些文字。我用下面的code对于使用我无法访问的单选的内线在 RadioGroup中使用Java。

如何设置的文字单选 RadioGroup中

XML布局:

 < RadioGroup中的android:layout_width =FILL_PARENT
    机器人:layout_marginLeft =20dip
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直
    机器人:ID =@ + ID / QueGroup1>    <单选机器人:检查=假
        机器人:按钮=@绘制/绿
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =FILL_PARENT
        机器人:文字颜色=#000
        机器人:文字=是
        机器人:ID =@ + ID / rbtnYes
        机器人:知名度=水涨船高/>    <单选机器人:检查=假
        机器人:按钮=@绘制/红
        机器人:文字颜色=#000
        机器人:文字=否
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =FILL_PARENT
        机器人:ID =@ + ID / rbtnNo
        机器人:知名度=水涨船高/>    <单选机器人:检查=假
        机器人:按钮=@绘制/红
        机器人:文字颜色=#000
        机器人:文字=不知道
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =FILL_PARENT
        机器人:ID =@ + ID / rbtnDontKnow
        机器人:知名度=水涨船高/>
< / RadioGroup中>

Java的code:

 私人无效fnRadioTextSet(字符串strval){
    rbtnYes =(单选)findViewById(R.id.rbtnYes);
    rbtnNo =(单选)findViewById(R.id.rbtnNo);
    rbtnDontKnow =(单选)findViewById(R.id.rbtnDontKnow);
    RadioGroup中rbtnGrp =(RadioGroup中)findViewById(R.id.QueGroup1);
    串[] strArrtext = strval.split(,);
    对(INT intcount = 0; intcount&下; strArrtext.length; intcount ++){
        rbtnGrp.getChildAt(intcount).settext(测试);
    }    // INT I = rbtnGrp.getChildCount();
    //Toast.makeText(getApplication(),rbtnGrp.getChildCount(),
    // Toast.LENGTH_LONG).show();
    / *的String [] = strText的strval.split();
    如果(strtext.length大于0){    } * /
}


解决方案

 私人无效fnRadioTextSet(字符串strval){
    RadioGroup中rbtnGrp =(RadioGroup中)findViewById(R.id.QueGroup1);
    串[] strArrtext = strval.split(,);
    的for(int i = 0; I< rbtnGrp.getChildCount();我++){
        ((单选按钮)rbtnGrp.getChildAt(ⅰ))的setText(strArrtext [I]);
    }
}

I have a RadioGroup inside of which I have some RadioButtons.

I want to set some text on the RadioButtons programmatically. I used following code for that using that I'm unable to access the RadioButtons inside of the RadioGroup using Java.

How can I set the text on a RadioButton inside a RadioGroup?

XML Layout:

<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 code:

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

这篇关于如何设置文本单选按钮这是一个RadioGroup中编程里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆