使用 Robotium 目前设置 NumberPicker值的最佳方法是什么?
使用Robotium 5.3.1,无法像以前的版本那样直接设置其值(使用 setNumberPicker 方法),那么,有没有解决方法呢?
编辑:在我的情况下,我在对话框中有2个NumberPicker和1个DatePicker。一旦我设置了DatePicker值,对话框就会被解除,所以我无法设置NumberPicker值。
xml布局是:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<NumberPicker
android:id="@+id/reminder_hour_numberpicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/task_details_item_duration_space"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/task_details_item_duration_space"
android:text=":"
android:textSize="@dimen/header"/>
<NumberPicker
android:id="@+id/reminder_minute_numberpicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<DatePicker
android:id="@+id/reminder_datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
</DatePicker>
</LinearLayout>
我的代码中的Robotium步骤是:
final DatePicker dpDate = (DatePicker) solo.getView(R.id.reminder_datePicker);
final DateTime dateTime = DateTime.now().plusDays(WHEN_TO_INTEGER.get(when));
solo.setDatePicker(dpDate, dateTime.getYear(), dateTime.getMonthOfYear() - 1, dateTime.getDayOfMonth());
solo.waitForView(R.id.reminder_hour_numberpicker);
solo.waitForView(R.id.reminder_minute_numberpicker);
final NumberPicker npHour = (NumberPicker) solo.getView(R.id.reminder_hour_numberpicker);
final NumberPicker npMinutes = (NumberPicker) solo.getView(R.id.reminder_minute_numberpicker);
final int displayedValueIndex = minutes / 15;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
npHour.setValue(hour);
npMinutes.setValue(displayedValueIndex);
}
});
答案 0 :(得分:1)
这可以解决您的问题:
使用getView
查找NumberPicker NumberPicker myNumberPicker = (NumberPicker) Solo.getView(R.id.xxxx);
使用以下方法设置其值
public void setNumberPicker(final NumberPicker numberPicker, final int value) {
if (numberPicker != null && getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
numberPicker.setValue(value);
}
});
}
}