Robotium DialogUtils「建议收藏」

Robotium DialogUtils「建议收藏」packagecom.robotium.solo;importandroid.app.Activity;importandroid.content.Context;importandroid.os.SystemClock;importandroid.view.ContextThemeWrapper;importandroid.view.View;impo

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

package com.robotium.solo;
import android.app.Activity;
import android.content.Context;
import android.os.SystemClock;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
* 弹框处理工具类
* Contains the waitForDialogToClose() method.
*
* @author Renas Reda, renas.reda@robotium.com
*
*/

class DialogUtils {
// activity操作工具类
private final ActivityUtils activityUtils;
// view获取工具类
private final ViewFetcher viewFetcher;
// 等待工具类
private final Sleeper sleeper;
// 1s
private final static int TIMEOUT_DIALOG_TO_CLOSE = 1000;
// 200ms
private final int MINISLEEP = 200;

/**
* 构造函数
* Constructs this object.
*
* @param activityUtils the {@code ActivityUtils} instance
* @param viewFetcher the {@code ViewFetcher} instance
* @param sleeper the {@code Sleeper} instance
*/

public DialogUtils(ActivityUtils activityUtils, ViewFetcher viewFetcher, Sleeper sleeper) {
this.activityUtils = activityUtils;
this.viewFetcher = viewFetcher;
this.sleeper = sleeper;
}

/**
* 检查在指定时间内弹框是否关闭了.
* Waits for a {@link android.app.Dialog} to close.
*
* @param timeout the amount of time in milliseconds to wait
* @return {@code true} if the {@code Dialog} is closed before the timeout and {@code false} if it is not closed
*/

public boolean waitForDialogToClose(long timeout) {
// 先等待弹框出现
waitForDialogToOpen(TIMEOUT_DIALOG_TO_CLOSE, false);
// 设置超时时间
final long endTime = SystemClock.uptimeMillis() + timeout;
// 循环检查弹框是否关闭了
while (SystemClock.uptimeMillis() < endTime) {

if(!isDialogOpen()){
return true;
}
// 等待200ms
sleeper.sleep(MINISLEEP);
}
return false;
}

/**
* 检查指定时间内,是否有弹框出现,
* timeout 设置的指定超时时间,单位 ms
* sleepFirst 是否需要先等待500ms,再做检查
* Waits for a {@link android.app.Dialog} to open.
*
* @param timeout the amount of time in milliseconds to wait
* @return {@code true} if the {@code Dialog} is opened before the timeout and {@code false} if it is not opened
*/

public boolean waitForDialogToOpen(long timeout, boolean sleepFirst) {
// 设置超时时间
final long endTime = SystemClock.uptimeMillis() + timeout;
// 是否需要等待500ms后再查找 (给Dialog启动的时间)
if(sleepFirst)
sleeper.sleep();
// 循环检查是否弹框出现了
while (SystemClock.uptimeMillis() < endTime) {

if(isDialogOpen()){
return true;
}
// 等待300ms
sleeper.sleepMini();
}
return false;
}

/**
* 检查是否有弹框出现
* Checks if a dialog is open.
*
* @return true if dialog is open
*/

private boolean isDialogOpen(){
// 获取当前显示的activity
final Activity activity = activityUtils.getCurrentActivity(false);
// 获取当前的所有DecorView类型View
final View[] views = viewFetcher.getWindowDecorViews();
// 获取最新的DecorView,DecorView是根
View view = viewFetcher.getRecentDecorView(views);
// 遍历检查是否有打开的弹框
if(!isDialog(activity, view)){
for(View v : views){
if(isDialog(activity, v)){
return true;
}
}
}
else {
return true;
}
return false;
}

/**
* 判断decorView是否是给定activity的,即检查弹框是否是当前activity的
* Checks that the specified DecorView and the Activity DecorView are not equal.
*
* @param activity the activity which DecorView is to be compared
* @param decorView the DecorView to compare
* @return true if not equal
*/

private boolean isDialog(Activity activity, View decorView){
// 检查decorView是都可见的,不可见直接返回false
if(decorView == null || !decorView.isShown()){
return false;
}
// 获取Context
Context viewContext = null;
if(decorView != null){
viewContext = decorView.getContext();
}
// 获取需要的Context
if (viewContext instanceof ContextThemeWrapper) {
ContextThemeWrapper ctw = (ContextThemeWrapper) viewContext;
viewContext = ctw.getBaseContext();
}
// 获取activity对应的Context
Context activityContext = activity;
Context activityBaseContext = activity.getBaseContext();
// 检查Context 是否是一致的,并且 activity不是在弹框中的
return (activityContext.equals(viewContext) || activityBaseContext.equals(viewContext)) && (decorView != activity.getWindow().getDecorView());
}

/**
* 隐藏软键盘
* editText 指定的编辑框
* shouldSleepFirst 是否要先等待500ms再操作
* shouldSleepAfter 执行完后是否要等待500ms再返回,仅对传入editText非null有效
* Hides the soft keyboard
*
* @param shouldSleepFirst whether to sleep a default pause first
* @param shouldSleepAfter whether to sleep a default pause after
*/

public void hideSoftKeyboard(EditText editText, boolean shouldSleepFirst, boolean shouldSleepAfter) {
// 获取当前activity
Activity activity = activityUtils.getCurrentActivity(shouldSleepFirst);
// 获取输入控制管理器服务
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
// 调用隐藏软键盘方法
if(editText != null) {
inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
return;
}
// 如果没有指定editText,获取当前焦点所在的View
View focusedView = activity.getCurrentFocus();
// 如果获取的 View不是EditText
if(!(focusedView instanceof EditText)) {
// 获取当前页面的最新 EditText
EditText freshestEditText = viewFetcher.getFreshestView(viewFetcher.getCurrentViews(EditText.class));
// 如果可以取到EditText那么设置可用的
if(freshestEditText != null){
focusedView = freshestEditText;
}
}
// 隐藏软键盘
if(focusedView != null) {
inputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
}
// 如果设置了等待,那么等待500ms后返回
if(shouldSleepAfter){
sleeper.sleep();
}
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160076.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月25日 下午5:00
下一篇 2022年7月25日 下午5:16


相关推荐

  • 回调(callback)

    回调(callback)1 什么是回调软件模块之间总是存在一定的接口 从调用方式上 可以把他们分为三类 同步调用 回调和异步调用 同步调用是一种阻塞式调用 调用方要等待对方执行完毕才返回 它是一种单向调用 回调是一种双向调用模式 也就是说 被调用方在接口被调用时也会调用对方的接口 异步调用是一种类似消息或事件的机制 不过它的调用方向刚好相反 接口的服务在收到某种讯息或发生某种事件时 会主动通知客户方 即调用客户方

    2026年3月17日
    2
  • 再次研究消息队列记的笔记——activemq

    再次研究消息队列记的笔记——activemq

    2021年7月12日
    81
  • cubieboard mysql_Cubieboard A10 安装Nand系统,配置nginx,php,mysql,samba详细教程[通俗易懂]

    cubieboard mysql_Cubieboard A10 安装Nand系统,配置nginx,php,mysql,samba详细教程[通俗易懂]安装前置条件1.下载win32diskimager-v0.7-binary.zip2.下载debian_wheezy_armhf_v1_mele.zip3.下载cubie_nand_uboot_partition_image.zip4.下载FlashFXP.zip5.下载PanasonicSDFormatter.zip6.下载puttyfile_0.62cn.zip以上文件下载地址:http://…

    2022年7月22日
    12
  • rapidgator怎么下载_他来时有闪电百度云

    rapidgator怎么下载_他来时有闪电百度云在国内,文件的分享基本上市通过百度云盘来的,但很多小伙伴有时候会发现需要下载的文件是放在一些国外云盘上的,这些文件该怎么下载捏?其实国外的云盘种类非常多,不像国内的一家独大,常用的国外云盘比如Rapidgator,alfafile,uploadgig,filespace等等。但这些云盘都是有些类似的特点就是,如果不付费下载的需要验证码或者下载的超级慢!!!尤其在国内的网络环境下。不仅如此,…

    2025年9月30日
    8
  • 计算机视觉-相机标定(Camera Calibration)

    计算机视觉-相机标定(Camera Calibration)1.相机标定基本原理1.1简介相机标定(Cameracalibration)简单来说是从世界坐标系换到图像坐标系的过程,也就是求最终的投影矩阵PPP的过程基本的坐标系世界坐标系:用户定义的三维世界的坐标系,为了描述目标物在真实世界里的位置而被引入。相机坐标系:在相机上建立的坐标系,为了从相机的角度描述物体位置而定义,作为沟通世界坐标系和图像/像素坐标系的中间一环。图像坐标系:为了描述成像过程中物体从相机坐标系到图像坐标系的投影透射关系而引入,方便进一步得到像素坐标系下的坐标。一般来说,标定

    2022年5月28日
    138
  • mac mysql改密码_mac系统重置密码

    mac mysql改密码_mac系统重置密码MAC重置MySql密码步骤:1.关闭mysql服务2.打开终端按步骤输入:输入1:cd/usr/local/mysql/bin/输入2:sudo./mysqld_safe–skip-grant-tables3.打开另外一个终端窗口:第一步输入:cd/usr/local/mysql/bin/第二步输入:./mysql第三步输入:FLUSHPRIVILEGES;第四步输入:ALTERUSER‘root’@‘localhost’IDENTIFIEDBY‘1

    2022年10月11日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号