gpt4 book ai didi

android - (Robotium) 如何选择 RadioGroup 中的 RadioButton

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:03:47 24 4
gpt4 key购买 nike

在我的 Android 应用 View 布局中,我有一个 <RadioGroup>其中包含两个 <RadioButton> :

<RadioGroup
android:id="@+id/my_radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<RadioButton
android:id="@+id/yes_btn"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/yes"
/>

<RadioButton
android:id="@+id/no_btn"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no"
/>
</RadioGroup>

我使用 Robotium 库为该单选按钮组编写 JUnit 测试 以选择一个单选按钮。测试代码 fragment 为:

Solo solo = new Solo(getInstrumentation(), getActivity()); 
...
solo.clickOnRadioButton(R.id.yes_btn); //I expect the "yes" radio button will be selected

我预计上面的测试代码会选择“YES”单选按钮,但是当运行它时,它引发了一个错误:

junit.framework.AssertionFailedError: 2131427675 RadioButtons are not found!
at com.jayway.android.robotium.solo.Waiter.waitForAndGetView(Waiter.java:417)
at com.jayway.android.robotium.solo.Clicker.clickOn(Clicker.java:374)
at com.jayway.android.robotium.solo.Solo.clickOnRadioButton(Solo.java:1063)
...

如何选择一个 <RadioButton><RadioGroup>然后用 Robotium ??

最佳答案

试试下面的代码

RadioButton rb = (RadioButton) solo.getView(R.id.yes_btn);
solo.clickOnView(rb);

当您传递资源 ID 时,solo.clickOnRadioButton() 将 View 索引作为参数。

关于android - (Robotium) 如何选择 RadioGroup 中的 RadioButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15830359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com