循环遍历 Android 视图的所有子视图? [英] Loop through all subviews of an Android view?

查看:21
本文介绍了循环遍历 Android 视图的所有子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款适用于 Android 的游戏.为了帮助实现它,我的想法是创建一个视图的子类.然后我会插入这个类的几个实例作为主视图的子视图.每个实例将处理检测何时被按下(通过 OnTouchListener).

I’m working on a game for Android. To help implement it, my idea is to create a subclass of a view. I would then insert several instances of this class as children of the main view. Each instance would handle detecting when it was pressed (via OnTouchListener).

我现在遇到的问题是如何遍历所有这些子视图,以便我可以读取它们的状态并处理它们?(即,当它们都达到某种状态时,应该会发生一些事情).

The problem I’m having now is how do I loop through all these sub-views so I can read their statuses and process them? (I.e. when they all reach a certain state something should happen).

或者有没有更好的方法让屏幕上的多个对象响应触摸以及我可以检查其状态?

Or is there a better way to have several objects on the screen that respond to touch and whose status I can check?

推荐答案

@jqpubliq 是对的,但如果你真的想浏览所有的视图,你可以简单地使用 getChildCount()来自 ViewGroup 的 getChildAt() 方法.一个简单的递归方法将完成剩下的工作.

@jqpubliq Is right but if you really want to go through all Views you can simply use the getChildCount() and getChildAt() methods from ViewGroup. A simple recursive method will do the rest.

这篇关于循环遍历 Android 视图的所有子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆