分享一个获取控件 id 较简单的方法

不说其他,直接上代码:
可以直接放在测试工程里用,记得改下包名。
5s刷新一次,需要自行在logcat窗口中设置筛选条件tag=getCurrentInfo 然后就~自行发挥吧

public class GetInfoTest extends ActivityInstrumentationTestCase2 {

    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "xxxxx.xxxxxxxx.xxxxxxxxxx.xxxxxxxxxx";
    private static Class launcherActivityClass;
    static {
        try {
            launcherActivityClass = Class
                    .forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public GetInfoTest() throws ClassNotFoundException {
        super(launcherActivityClass);
    }
    private Solo solo;
    protected void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

    public void tearDown() throws Exception {
    }
    public void testGetInfo(){
        while(true){
            solo.sleep(5000);
            getCurrentInfo();
        }   
    }
    public void getCurrentInfo(){
        Log.v("getCurrentInfo", "current activity is "
                + solo.getCurrentActivity().getClass().getSimpleName());

        ArrayList<View> av = solo.getCurrentViews();
        Log.v("getCurrentInfo", "begin get view info");
        for (View view : av) {
            Log.v("getCurrentInfo", "* * * * * * *");
            int[] location = { 0, 0 };
            view.getLocationInWindow(location);
            Log.v("getCurrentInfo", "location:" + location[0] + ","
                    + location[1]);
            Log.v("getCurrentInfo", "name:" + view.getClass().getName());
            int id = view.getId();
            if (view instanceof TextView) {
                Log.v("getCurrentInfo", "text:" + ((TextView) view).getText().toString());
            }
            if (view instanceof ViewGroup) {
                Log.v("getCurrentInfo", "this view is a viewgroup");
            }
            if (id == -1)
                continue;
            if (null != view.getResources()
                    && null != view.getResources().getResourceEntryName(id))
                Log.v("getCurrentInfo", "id:"+view.getResources().getResourceEntryName(id)
                        + ",resourse:" + Integer.toHexString(id));
                Log.v("getCurrentInfo", "clickable:"+view.isClickable());
                Log.v("getCurrentInfo", "Enable:"+view.isEnabled());
        }

        ArrayList<WebElement> WebElementsList = solo.getCurrentWebElements();
        Log.v("getCurrentInfo", "begin get webelements");
        for (WebElement we : WebElementsList) {
            Log.v("getCurrentInfo", "classname is " + we.getClassName());
            Log.v("getCurrentInfo", "id is " + we.getId());
            Log.v("getCurrentInfo", "x is " + we.getLocationX());
            Log.v("getCurrentInfo", "y is " + we.getLocationY());
            Log.v("getCurrentInfo", "name is " + we.getName());
            Log.v("getCurrentInfo", "tag name is " + we.getTagName());
            Log.v("getCurrentInfo", "text is " + we.getText());
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值