Android开发:如何获取ViewGroup的所有EditText子级?

发布于 2024-10-31 20:04:22 字数 264 浏览 1 评论 0原文

基本上,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒眉千度 2024-11-07 20:04:22
 LinearLayout ll = //Your Layout this can be any Linear or Relative layout 
                     //in which you added your spinners at runtime ;

    int count = ll.getChildCount();
    for(int i =0;i<count;i++)
    {
        View v = ll.getChildAt(i);
        if(v instanceof Spinner)
        {
            // you got the spinner
            EditText s = (EditText) v;
            Log.i("Item selected",s.getText().toString());
        }
    }
 LinearLayout ll = //Your Layout this can be any Linear or Relative layout 
                     //in which you added your spinners at runtime ;

    int count = ll.getChildCount();
    for(int i =0;i<count;i++)
    {
        View v = ll.getChildAt(i);
        if(v instanceof Spinner)
        {
            // you got the spinner
            EditText s = (EditText) v;
            Log.i("Item selected",s.getText().toString());
        }
    }
旧人哭 2024-11-07 20:04:22

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 methods ViewGroup.getChildCount() and ViewGroup.getChildAt(int) useful for this.

简美 2024-11-07 20:04:22

您需要在每个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文