如何进行 10 次快速点击 [UIautomator]

How to make 10 fast clicks [UIautomator]

我正在尝试使用此按钮快速点击 10 次

public static void fastClicks(String text, int index) throws Exception {
        Thread.sleep(1000);
        UiObject settingsButton = new UiObject(new UiSelector().resourceId(text).index(index));
        Configurator cc = Configurator.getInstance();
        cc.setActionAcknowledgmentTimeout(10);
          for (int i = 1; i < 11; ++i){
        settingsButton.click();
        System.out.println("clicked "+ i + " ");
    }
    }

是的,它点击了 10 次,但第一次点击有一点延迟或类似的东西,所以它不能正常工作。我只需要 10 次 ritmic 点击,从 1 次点击到 10 次相同的延迟。我该如何改进这段代码?谢谢:)

否则我试过这个代码

 public static void fastClicks(String text, int index, int clicksCount) throws Exception {
        UiObject settingsButton = new UiObject(new UiSelector().resourceId(text).index(index));
        for(int currentClickIndex = 0; currentClickIndex < clicksCount; currentClickIndex++) {
            if(settingsButton.exists()) {
                settingsButton.click();
                Thread.sleep(40);
                System.out.println("clicked " + currentClickIndex + " times");
            }
        }
    }

仍然没有。

抱歉,我没有足够的声誉来发表评论,所以我会尽量做出正确的回答。

因为这种行为只会在第一次点击时出现,它可能是因为某些配置是在操作本身之前(或之后)进行的。例如:

https://android.googlesource.com/platform/frameworks/testing/+/master/uiautomator/library/core-src/com/android/uiautomator/core/UiObject.java

public boolean click() throws UiObjectNotFoundException {
    [...]
    AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
    [...]
}

protected AccessibilityNodeInfo findAccessibilityNodeInfo(long timeout) {
    [...]
    while (currentMills <= timeout) {
        node = getQueryController().findAccessibilityNodeInfo(getSelector());
        if (node != null) {
            break;
        } else {
            // does nothing if we're reentering another runWatchers()
            UiDevice.getInstance().runWatchers();
        }
       [...]
    }
    return node;
}

为避免这种情况,您可以尝试先获取对象的边界,然后直接调用 getUiDevice().click(...)

    UiObject settingsButton = new UiObject(new UiSelector().resourceId(text).index(index));
    Rect bounds = settingsButton.getBounds();
    for (int i = 1; i < 11; ++i){
         getUiDevice().click(bounds.centerX(), bounds.centerY());
         System.out.println("clicked "+ i + " ");
    }

(由@Rami Kuret 建议 )