我有一个视图,其中我动态添加了几个Edittext视图,并且我需要循环查看这些视图才能捕获输入的任何数据. 我在StackOverFlow上查看过这个解决方案:

public void recursiveLoopChildren(ViewGroup parent) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            final View child = parent.getChildAt(i);
            if (child instanceof ViewGroup) {
                recursiveLoopChildren((ViewGroup) child);
                // DO SOMETHING WITH VIEWGROUP, AFTER CHILDREN HAS BEEN LOOPED
            } else {
                if (child != null) {
                    // DO SOMETHING WITH VIEW
                }
            }
        }
    }

但这不起作用,因为ViewGroup不包含方法GetChildCount(). 我看过ViewGroupView的定义,两者都没有"计数"或"getchildnum"方法或属性. 有没有方法循环查看视图的子视图?

推荐答案

嗯,ViewGroupView都没有子元素数,但LinearLayout有. 鉴于此,我能够完成这件事.

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="wrap_content"
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_marginRight="34.4dp">
            <Button
                android:layout_marginLeft="3dp"
                android:background="@color/colorGold"
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/saveChange"
                android:id="@+id/btnSave" />
      </LinearLayout>
    </HorizontalScrollView>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_gravity="center"
        android:layout_marginTop="0sp"
        android:background="#000000" />
    <ImageView
        android:contentDescription="Participant's Photo"
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:id="@+id/imageview_participant"
        android:layout_marginBottom="0.0dp" />
    <TextView
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:gravity="center"
        android:id="@+id/TextView_name" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold"
            android:layout_width="230dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:id="@+id/ageasof"
            android:text="@string/ageAsOf" />
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/age"
            />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_gravity="center"
        android:layout_marginTop="0sp"
        android:background="#000000" />
    <TextView
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:gravity="center"
        android:text="@string/AdditionalData" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/listlayout">
    </LinearLayout>
</LinearLayout>

As you can see I have three LinearLayouts of which only one (listlayout) has the subviews I'm looking for because they are added to the listlayout LinearLayout at run time. I am able to loop through each of the LinearLayouts and access the EditTexts contained within. I use a button click to save the data.
My code:

btnSave.Click += (sender, e) =>
{
    for (int i = 0; i < list.ChildCount; i++)
    {
        object MyObject = list.GetChildAt(i);
        if (MyObject.GetType() == typeof(LinearLayout))
        {
            LinearLayout MyLinearLayout = (LinearLayout)MyObject;
            for (int j= 0; j < MyLinearLayout.ChildCount; j++)
            {
                object MyObject2 = MyLinearLayout.GetChildAt(j);
                if (MyObject2.GetType() == typeof(EditText))
                {
                    EditText MyEditText = (EditText)MyObject2;
                    datainterface.SaveDataFieldData(MyUser.Username, MyUser.Password, fieldid, (string)MyEditText.Tag, MyEditText.Text);
                }
            }
        }
    }
};

我将数据字段ID存储在EditText的标签中,以便在检索数据进行编辑时将数据与正确的字段对齐. 是的,我可以进一步优化代码,但我想按原样发布它,这样它更具可读性. 无论如何,它的作用就像一种魅力.

Csharp相关问答推荐

C#中的Valkey缓存

在包含空项的列表上使用具有断言T的摘要表

在C#中使用in修饰符

需要深入了解NpgSQL DateTimeOffset处理

数组被内部函数租用时,如何将数组返回给ArrayPool?

Appsettings.json未加载.Net 8 Blaazor Web程序集

静态对象构造顺序

在ASP.NET Core Web API项目中通过中间件修改`Request.Path`不会更改使用的控制器/操作

TCPClient阅读流

如何在毛伊岛应用程序中完美地同步视图模型和视图的加载?

NET8 MAUI并部署到真实设备上进行测试

如何使用XmlSerializer序列化带有CDATA节的XML文件?

BlockingCollection T引发意外InvalidOperationException

从另一个不同 struct 的数组创建Newtonsoft.Json.Linq.J数组

为什么我可以用硬编码的集合而不是变量来设置没有setter的IList属性的值?

HttpClient SendAsync与多线程环境中的ArchiveZip

C#中使用ReadOnlySpan的泛型类型推理

Autofac -动态实例化:手动传递构造函数

如何在Xamarin.Forms中检索PanGesture事件的位置?

如何读取TagHelper属性的文本值?