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();
}
}
}

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • STM32–RFID无线射频技术(RC522刷卡模块)

    STM32–RFID无线射频技术(RC522刷卡模块)STM32+RC522刷卡模块,亲测可用!!!

  • String与Integer相互转换「建议收藏」

    String与Integer相互转换「建议收藏」String与Integer相互转换//方法一:Integer类的静态方法toString()Integera=2;Stringstr=Integer.toString(a)//方法二:Integer类的成员方法toString()Integera=2;Stringstr=a.toString();//方法三:String类的静态方法valueOf()…

  • JS-jquery 获取当前点击的对象

    JS-jquery 获取当前点击的对象

    2021年10月31日
  • 超级简单的matplotlib安装教程

    超级简单的matplotlib安装教程网上许多matplotlib的安装教程都是比较复杂,需要配置许多环境,对于电脑基础不好的人来说可是一件头疼的事情,今天我介绍一个简单的安装方法。1.Win+R输入cmd进入到CMD窗口下,执行python-mpipinstall-Upipsetuptools进行升级。2.输入python-mpipinstallmatplotlib进行自动的安装,系统会自动下载安装包…

  • 再次推荐一款逼真的HTML5下雪效果

    效果图:效果描述:之前推荐过一款下雪的jQuery插件之前的那款下降速度比较缓慢,今天推荐的这个下降速度比较快,大雪哇使用方法:1、将index.html中的样式复制到你的样式表中2、将body中的代

    2021年12月22日
  • VMM callback「建议收藏」

    VMM callback「建议收藏」vmm的callback调用的是基类,而不是扩展类。这样做的目的,就是不修改原程序结构的基础上来增加或者替换内容,大大的提高了重用性。修改的内容在扩展类中增添。这种思想应该来源于SV中的虚方法,虚方法可以重写其所有基类中的方法,然普通的重写只能在本身及其扩展类中有效。这样就可以调用基类的时候,访问到扩展类中对基类中虚函数的重写。由虚函数发展到虚类,虚类是一个定义抽象概念的类模板,不可以实…

发表回复

您的电子邮箱地址不会被公开。

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