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);
}
親測有效