---
title: "UIAutomator的API 学习小例子"
author: "jerome_wang"
date: "2015-07-14T14:08:51Z"
tags: []
categories: ["移动开发"]
slug: "uiautomator-api"
draft: false
lastmod: "2015-07-14T14:08:51Z"
id: 2227188
url: "https://jerome-wang.iteye.com/blog/2227188"
---

在模拟器上玩的

1.代码 

import com.android.uiautomator.core.UiDevice;

import com.android.uiautomator.core.UiObject;

import com.android.uiautomator.core.UiObjectNotFoundException;

import com.android.uiautomator.core.UiScrollable;

import com.android.uiautomator.core.UiSelector;

import com.android.uiautomator.testrunner.UiAutomatorTestCase;

 

public class GetDevice extends UiAutomatorTestCase {

 

public void test() {

 

try {

UiDevice testDevice = getUiDevice();

testDevice.pressHome();

UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));

allAppsButton.clickAndWaitForNewWindow();

     UiScrollable appViews = new UiScrollable( new UiSelector().className("android.view.View"));//获取apps这个控件

     appViews.setAsHorizontalList();//设置水平移动

     for (int i=0; i&lt;appViews.getMaxSearchSwipes() - 1;i++) { //找到settings 并点击

 

     UiObject snapeaApp = new UiObject(new UiSelector().text("Settings"));

       if (snapeaApp.exists()) {

          snapeaApp.clickAndWaitForNewWindow();

          break;

     }

     }

} catch (UiObjectNotFoundException e) {

e.printStackTrace();

}

 

}

}

2.执行步骤

![](http://dl2.iteye.com/upload/attachment/0110/1469/a942f778-4ff1-3c0c-9783-7c41f3071886.png)