嗯,ViewGroup
或View
都没有子元素数,但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 LinearLayout
s 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 LinearLayout
s and access the EditText
s 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
的标签中,以便在检索数据进行编辑时将数据与正确的字段对齐. 是的,我可以进一步优化代码,但我想按原样发布它,这样它更具可读性. 无论如何,它的作用就像一种魅力.