getChildCount()返回不正确的孩子数量

编程入门 行业动态 更新时间:2024-10-27 15:19:10
本文介绍了getChildCount()返回不正确的孩子数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用X按钮创建了一个自定义TextView,单击该按钮后其可见性设置为GONE.现在,我想获得LinearLayout中可见的TextViews的数量.目前,我正在获取插入的TextView总数,而不是可见的总数.

I have created a custom TextView with an X button, whose visibility are set to GONE when the button is clicked. Now I want to get the number of visible TextViews in the LinearLayout. Currently, I am getting the count of total TextViews inserted rather than the visible ones.

示例:

当我有2个TextViews时,getChildCount()给出2个 但是如果我通过单击X按钮删除一个TextView,它仍然会给我2.为什么会发生这种情况?

When I have 2 TextViews, getChildCount() gives 2 but if I delete one TextView by clicking the X button, it still gives me 2. Why is this happening?

我已经创建了这样的东西:

I have created something like this:

这里的X是一个按钮,其onClick()会将TextView和Button的可见性都设置为GONE.

The X here is a button whose onClick() will set the visibility of both TextView and the Button to GONE.

推荐答案

如何获得可见孩子的数量?

how can I get the count of the visible children?

为此,您需要遍历视图/布局的子级并检查可见性.这是一个简单的循环:

Well for that you need to iterate over the children of the view/layout and check the visibility. It is a simple loop:

// untested/pseudocode int visibleChildren = 0; for (int i = 0; i < layout.getChildCount(); i++) { if (layout.getChildAt(i).getVisibility() == View.VISIBLE) { visibleChildren++; } }

更多推荐

getChildCount()返回不正确的孩子数量

本文发布于:2023-11-11 04:51:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577428.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正确   数量   孩子   getChildCount

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!