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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • c语言中结构体的指针初始化,c语言结构体指针初始化

    c语言中结构体的指针初始化,c语言结构体指针初始化今天终于看完了 C 语言深度剖析这本书 对 C 语言有了进一步的了解与感悟 突然发觉原来自己学 C 语言的时候学得是那样的迷糊 缺少深入的思考 在重新看书的时候发觉 C 语言基本教材虽然经典 但是缺乏独到性 老师在讲解的过程中也就照本宣科了 没有多大的启迪 看到 C 语言内存管理这块 发觉还是挺有用的 当然平时在编程时基本上就没有考虑过内存问题 定义了指针变量 没有为指针分配内存 即指针没有在内存中指向一块合法的内存

    2025年11月11日
    2
  • tail -f 命令详解

    tail -f 命令详解tail命令可用于查看文件的内容,有一个常用的参数-f常用于查阅正在改变的日志文件。tail-f等同于–follow=descriptor,根据文件描述符进行追踪,当文件改名或被删除,追踪停止tail-F等同于–follow=name–retry,根据文件名进行追踪,并保持重试,即该文件被删除或改名后,如果再次创建相同的文件名,会继续追踪t…

    2022年5月29日
    94
  • np管理器去更新(npx命令)

    一、npm查看某个模块的版本信息,例如element框架npminfoelement-ui二、npm更新模块到最新版本npminstallelement-ui@latestnpm更新模块到某个版本npminstallelement-ui@2.12.0更多:vs2019中怎么把tab补全换成空格补全;vs2019如何关闭空格自动补…

    2022年4月18日
    149
  • 自动化运维平台的流程草图「建议收藏」

    自动化运维平台的流程草图「建议收藏」对于平台里面的几个地方一直没大理清楚,所以想了几种办法,一种是蒙着头继续做,想到哪里做到哪里,结果做的时候发现很多东西都没有规划好,很容易从这个死胡同调入另一个死胡同,所以进度不能保证,质量不能保证。 第二个是…

    2022年5月16日
    45
  • LINUX系统更改系统时区

    LINUX系统更改系统时区Linux-Redhat系统更改系统时区[root@localhost~]#date #查看本地时间ThuMay1923:41:32EDT2022[root@localhost~]#hwclock–show #查看硬件时间Thu19May202211:42:07PMEDT-0.332325seconds当前系统版本:RedHatEnterpriseLinuxServerrelease7.0(Maipo)查看目前的时区:[r

    2025年6月24日
    1
  • 【原创】纯干货,Spring-data-jpa详解,全方位介绍。

    【原创】纯干货,Spring-data-jpa详解,全方位介绍。本篇进行Spring-data-jpa的介绍,几乎涵盖该框架的所有方面,在日常的开发当中,基本上能满足所有需求。这里不讲解JPA和Spring-data-jpa单独使用,所有的内容都是在和Spring整合的环境中实现。如果需要了解该框架的入门,百度一下,很多入门的介绍。在这篇文章的接下来一篇,会有一个系列来讲解mybatis,这个系列从mybatis的入门开始,到基本使用,和spring整合,和第…

    2022年6月10日
    28

发表回复

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

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