Robotium 似乎没有显示键盘的方法。此外,键盘似乎超出了 Robotium 可以交互的范围,clickOnText()
即使它是可见的,也不会按下软键盘按钮。因此,这个答案将是一个黑客攻击。
解决方案有两个重要部分。首先,虽然我们不能dispatchKeyEvent
像使用其他键盘按钮那样直接单击 IME Next 按钮,但我们可以使用 触发它的回调EditText.onEditorAction(EditorInfo.IME_ACTION_NEXT)
。这将允许我们跳到下一个 EditText。其次,触发这个回调属于“与UI交互”的范畴,所以我们必须从运行Robotium的线程移回主线程进行调用。我们将使用它Activity.runOnUiThread()
来实现这一点。
这是它如何为我工作的示例:
public void testImeNext() throws Exception
{
//Grab a reference to your EditText. This code grabs the first Edit Text in the Activity
//Alternatively, you can get the EditText by resource id or using a method like getCurrentEditTexts()
//Make sure it's final, we'll need it in a nested block of code.
final EditText editText = solo.getEditText(0);
//Create a runnable which triggers the onEditorAction callback
Runnable runnable = new Runnable()
{
@Override
public void run()
{
editText.onEditorAction(EditorInfo.IME_ACTION_NEXT);
}
};
//Use Solo to get the current activity, and pass our runnable to the UI thread.
solo.getCurrentActivity().runOnUiThread(runnable);
}