【问题标题】:Display button text from from selected index in a RadioGroup显示来自 RadioGroup 中选定索引的按钮文本
【发布时间】:2016-01-14 10:46:32
【问题描述】:

我的应用程序会自动填充一封电子邮件,其中包含用户通过表单输入的信息。当我尝试显示他们选择的索引时,它给了我

android.support.v7.widget.AppCompatRadioButton {2d57753c VFED..C. ...P..ID 705,0-903,96#7f0d0056 app:id/RejectRadio}

这是正确的 id 等,但我宁愿它只说“拒绝”,或者如果选择其他按钮只是“接受”。我该怎么做。

XML

<RadioGroup
   android:id="@+id/radiochoice"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_below="@+id/EditTextFeedbackBody"
   android:orientation="horizontal">

        <RadioButton
            android:id="@+id/AcceptRadio"
            android:layout_width="wrap_content"
            android:layout_marginStart="50dp"
            android:layout_height="wrap_content"
            android:text="@string/acceptbutton" />

        <RadioButton
            android:id="@+id/RejectRadio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rejectbutton"
            android:layout_marginStart="115dp" />
</RadioGroup>

JAVA

int radioButtonID = radiochoice.getCheckedRadioButtonId();
View radioButton = radiochoice.findViewById(radioButtonID);
int index = radiochoice.indexOfChild(radioButton);

final RadioGroup ApproveorReject = (RadioGroup) findViewById(R.id.radiochoice);
fAnswer = ApproveorReject.getChildAt(index).toString();

【问题讨论】:

    标签: java android xml radio-button android-radiogroup


    【解决方案1】:

    获取单选按钮的索引后,您需要获取文本:

    RadioButton btn = (RadioButton) radioButtonID.getChildAt(radioButtonId);
    fAnswer = (String) btn.getText();
    

    【讨论】:

      【解决方案2】:

      您是否尝试获取单选按钮的文本?

       RadioButton btn = (RadioButton) ApproveorReject.getChildAt(index);
       fAnswer  = (String) btn.getText();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-27
        • 2016-08-14
        • 1970-01-01
        • 2012-04-08
        • 1970-01-01
        • 2017-08-12
        • 2021-04-14
        相关资源
        最近更新 更多