【问题标题】:Android Uiautomator not recognized the programatically view idAndroid Uiautomator 无法以编程方式识别视图 ID
【发布时间】:2015-07-05 22:41:18
【问题描述】:

我有使用 Java 构建的布局 - 不是来自 XML 文件。

我通过 Java 代码为每个视图分配一个任意 id:

例如:

Button btn = new Button(this);
btn.setText("testBtn");
btn.setId(123456);

问题是来自 Android 工具的 uiautomator 无法识别 id - 我得到空 id。

当我使用 XML 文件添加按钮时,一切正常。

所以我的问题是,id的动态设置有什么问题, 两者有什么变化,为什么 uiautomator 不能识别动态 id?

附:目前我不喜欢将所有布局更改为 XML 的

【问题讨论】:

    标签: android view android-uiautomator


    【解决方案1】:

    这是UIAutomator 的限制。UIAutomator 只检测XML layouts 中列出的对象

    【讨论】:

    • 您好,感谢您的回答。即使UIAutomator 没有“看到”它们,您是否知道 Appium 脚本是否可以使用这些 id?你知道 Appium 检查器是否检测到单个视图的所有参数,包括他的 id?
    • 我不太确定Appium inspector,但我相信它与Hierarchy viewer 相同,如果是——即使它无法检测到它..
    【解决方案2】:

    UIAutomator 将无法识别在步骤 1 中直接设置的小部件的动态 ID,而是我们必须维护未使用 ID 的 xml 文件并在 setID() 中使用未使用的 ID,如在步骤 2 中。

    step1:
    
    Button btn = new Button(this);
    btn.setText("testBtn");
    btn.setId(123456);
    
    step2:
    
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <item name="newID" type="id"/>
    </resources>
    
    Button btn = new Button(this);
    btn.setText("testBtn");
    String packageName = getPackageName();
    int resId = getResources().getIdentifier("newID", "id", packageName);
    btn.setId(resId);
    
    The above approach works cool.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多