Android uiautomator2 LongClick不生效問題解決

Android 的UiObject提供了長按的操作,但是我在實(shí)際使用中卻發(fā)現(xiàn)還是執(zhí)行的單擊操作,應(yīng)該是默認(rèn)的接口函數(shù)長按的時(shí)間太短導(dǎo)致沒有觸發(fā)我的長按事件

之前我想的Android會(huì)不會(huì)提供官方API可以設(shè)置長按時(shí)間呢,找了一圈發(fā)現(xiàn)并沒有

后面也是在網(wǎng)上找到了別人的處理方法,這種方法也是聰明到點(diǎn)兒了

public void longClick(UiDevice ud, UiObject uiObject,int steps) throws UiObjectNotFoundException{

ud.swipe(uiObject.getBounds().centerX(), uiObject.getBounds().centerY(),uiObject.getBounds().centerX(),uiObject.getBounds().centerY(), steps);

}


這種方法實(shí)際使用到了swipe方法,只是swipe的起點(diǎn)與終點(diǎn)相同,然后step步設(shè)置久一點(diǎn),就會(huì)在這個(gè)控制長按

另外官方文檔中說1step的時(shí)間是5ms,所以我們還可以修改上面方法,參數(shù)直接傳時(shí)間


public void longClick(UiDevice ud, UiObject uiObject,int ms) throws UiObjectNotFoundException{

int step = ms/5;

ud.swipe(uiObject.getBounds().centerX(), uiObject.getBounds().centerY(),uiObject.getBounds().centerX(),uiObject.getBounds().centerY(), step);

}


親測有效

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容