Android开发:如何获取ViewGroup的所有EditText子级?
基本上,我有一个 LinearLayout,它包含随机数量的水平 LinearLayout,并且在每个水平 LinearLayout 中都有一个 TextView 和一个 EditText。我希望能够获取主 LinearLayout 的每个 EditText 子级的值。
抱歉,如果有点令人困惑,我不擅长解释事情!
我可以为每个 EditText 设置相同的 ID,然后使用 findViewById,还是只返回 EditText 的第一个实例?
谢谢, 亚历克斯.
Basically, I have a LinearLayout that holds a random amount of horizontal LinearLayouts, and in each of the horizontal LinearLayouts there's a TextView and an EditText. I want to be able to get the value of each EditText children of the master LinearLayout.
Sorry if it's confusing, I'm no good at explaining things!
Could I just set the same ID for each of the EditTexts then use findViewById, or would that only return the first instance of an EditText?
Thanks,
Alex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
findViewById
仅返回具有给定 id 的第一个视图。您将必须自己遍历视图层次结构,至少直到您深入到每个水平线性布局为止。您会发现ViewGroup.getChildCount()
和ViewGroup.getChildAt(int)
方法对此很有用。findViewById
returns only the first view with the given id. You're going to have to traverse the view hierarchy yourself, at least until you get down to each horizontal linear layout. You'll find the methodsViewGroup.getChildCount()
andViewGroup.getChildAt(int)
useful for this.您需要在每个 LinearLayout 上调用 findViewById。如果这样做,则可以为每个 EditText 设置相同的 ID。
You would need to call findViewById on each of the LinearLayouts. If you do this, you can set the same ID for each EditText.