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


相关推荐

  • 【转载】一些VS2013的使用技巧

    【转载】一些VS2013的使用技巧

    2021年11月18日
    39
  • 一些有用的电驴网址

    一些有用的电驴网址
    0、http://www.emule-project.net/
    1、http://www.simplecd.org/
    2.http://qvocd.com/
    3、http://www.ied2k.com/
    4、http://www.thshare.net/
    5、http://www.eastgame.net 
    eastgame.net 
    6、http://www.chnp2p.com/
    7、http://www.h

    2022年7月15日
    24
  • Flink学习之flink sql「建议收藏」

    Flink学习之flink sql「建议收藏」???? 昨天我们学习完TableAPI后,今天我们继续学SQL,TableAPI和SQL可以处理SQL语言编写的查询语句,但是这些查询需要嵌入用Java、Scala和python编写的程序中。hadoop专题:hadoop系列文章.spark专题:spark系列文章.flink专题:Flink系列文章.????只需要具备SQL的基础知识即可,不需要其他编程经验。我的SQL客户端选择的是docker安装的FlinkSQLClick,大家根据自己的需求安装即可。目录1.1.

    2022年8月20日
    7
  • java 加载.ftl_如果加载jar里面的ftl文件

    java 加载.ftl_如果加载jar里面的ftl文件我将ftl及一些基础类打成了一个java,用maven的方式依赖,结果调用的时候调用不了jar中的ftl文件,找的还是调用方的地址去找ftl文件2019-07-0512:10:00,086[FailProcessor][WARN]Error@/manage/login:java.lang.RuntimeException:freemarker.template.TemplateNotF…

    2022年6月18日
    63
  • tpshop带微分销_TPshop微分销商城有什么作用[通俗易懂]

    tpshop带微分销_TPshop微分销商城有什么作用[通俗易懂]TPshop微分销商城有什么作用?据你所了解的有多少?一、可以简化商品的购物流程我们都知道的是在实体店进行购买的时候,我们可以直接看到商品,并且能够摸到产品的质量如何,并且在最后,假如并不合适的时候,是可以找商家进行退还的,但是网上购买就有所不同,有的时候还有些复杂,所以微商分销商品也着力于这一点进行考虑。适当的简化了商品的购物流程,让整个购物旅程更加美好。二、通过这一点吸引了浩繁的粉丝进行了最低…

    2022年5月16日
    41
  • tomcat部署war包,jar包

    tomcat部署war包,jar包在tomcat根目录下部署war包,通过http://ip:port的方式访问网上说的方法很多,这里记录一下自己的经过实践检验的一种方法。1、先去tomcat/conf/server.xml里有一个HOST标签,有个参数appBase,表示你的应用应该部署在什么位置。例如下面写的是webapps,那就在webapps/ROOT/路径下解压你的war包。如果appBase=XXX,那么就…

    2022年6月4日
    18

发表回复

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

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