gpt4 book ai didi

android - 如何在动态 View 中添加/获取数据?

转载 作者:行者123 更新时间:2023-11-29 18:46:14 25 4
gpt4 key购买 nike

我有id为“layout”的父线性布局(它在 ScrollView 中),在这个父布局(线性布局)中,我生成自定义约束布局,它有很多 View ( TextView 、图像、按钮、编辑文本等)。

这是父 View 的布局:

 <?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/linear">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:fillViewport="true"
android:id="@+id/scroll">


<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/layout"
android:orientation="vertical"
<!--inside it will be many constraint view childs-->
/>
</ScrollView>

<Button
android:layout_width="70dp"
android:layout_height="30dp"
android:background="@drawable/red_rounded_btn"
android:layout_margin="8dp"
android:padding="4dp"
android:id="@+id/finish_btn"
android:textColor="@color/colorPrimary"
android:layout_gravity="right"
android:gravity="center_horizontal"
android:text="Finish" />
</LinearLayout>

这是我的自定义约束布局(子):

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/child">


<ImageView
android:id="@+id/imageView_field"
android:layout_width="138dp"
android:layout_height="138dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toStartOf="@+id/uploud_field_pic_btn"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/sym_def_app_icon" />

<Button
android:id="@+id/uploud_field_pic_btn"
android:layout_width="185dp"
android:layout_height="35dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="164dp"
android:background="#1aB00020"
android:drawableRight="@drawable/uploud_icon"
android:text="@string/insert_picture"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView_size_custom_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/size"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/custom_name_reg"
app:layout_constraintTop_toBottomOf="@+id/custom_name_reg" />

<EditText
android:id="@+id/textView_size_in_reg_custom"
android:layout_width="226dp"
android:layout_height="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:paddingLeft="5dp"

android:layout_marginTop="12dp"
android:background="@color/colorPrimary"
android:inputType="number"
android:hint="field size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/custom_name_in_reg" />

<TextView
android:id="@+id/textView_price_custom_reg"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:layout_marginTop="16dp"
android:text="@string/price"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/textView_price_in_custom_reg"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/textView_size_custom_reg"
app:layout_constraintTop_toBottomOf="@+id/textView_size_custom_reg" />

<EditText
android:id="@+id/textView_price_in_custom_reg"
android:paddingLeft="5dp"
android:layout_width="226dp"
android:layout_height="24dp"
android:layout_marginTop="8dp"
android:inputType="number"
android:hint="price/hr"
android:background="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="@+id/textView_size_in_reg_custom"
app:layout_constraintTop_toBottomOf="@+id/textView_size_in_reg_custom" />

<TextView
android:id="@+id/textView_available_day_custom_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/availability_days"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/textView_price_custom_reg"
app:layout_constraintTop_toBottomOf="@+id/textView_price_custom_reg" />

<EditText
android:id="@+id/textView_available_day_in_custom_reg"
android:layout_width="226dp"
android:layout_height="24dp"
android:layout_marginTop="8dp"
android:background="@color/colorPrimary"
android:hint="ex: (sun-wed)-mon"
android:paddingLeft="5dp"
app:layout_constraintEnd_toEndOf="@+id/textView_price_in_custom_reg"
app:layout_constraintTop_toBottomOf="@+id/textView_price_in_custom_reg" />

<TextView
android:id="@+id/textView_available_time_custom_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/availability_time"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/textView_available_day_custom_reg"
app:layout_constraintTop_toBottomOf="@+id/textView_available_day_custom_reg" />

<EditText
android:id="@+id/textView_available_time_in_custom_reg"
android:layout_width="226dp"
android:layout_height="21dp"
android:layout_marginTop="8dp"
android:background="@color/colorPrimary"
android:hint="ex:(8am-11pm)-(2pm-5pm)"
android:paddingLeft="5dp"
app:layout_constraintEnd_toEndOf="@+id/textView_available_day_in_custom_reg"
app:layout_constraintTop_toBottomOf="@+id/textView_available_day_in_custom_reg" />

<TextView
android:id="@+id/textView_to_be_filled_custom_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/custom_name_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="72dp"
android:layout_marginRight="72dp"
android:layout_marginTop="72dp"
android:text="@string/name"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/custom_name_in_reg"
app:layout_constraintTop_toBottomOf="@+id/imageView_field" />

<EditText
android:id="@+id/custom_name_in_reg"
android:layout_width="226dp"
android:layout_height="20dp"
android:paddingLeft="5dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="24dp"
android:background="@color/colorPrimary"
android:ems="10"
android:hint="field name /number"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/uploud_field_pic_btn" />
</android.support.constraint.ConstraintLayout>

我动态生成此自定义约束 View ,多次在我的 Activity 类中的线性 View 父级内线性生成,如下所示:

    for (int i = 0; i < f; i++) {

// Add the text layout to the parent layout
view = layoutInflater.inflate(R.layout.custom_owner_register_layout, parentLayout, false);

// In order to get the view we have to use the new view with text_layout in it
ConstraintLayout constraintLayout = (ConstraintLayout) view.findViewById(R.id.child);

TextView textView = (TextView) view.findViewById(R.id.textView_to_be_filled_custom_reg);
textView.setText("Field # " + (i + 1));

// Add the text view to the parent layout
parentLayout.addView(constraintLayout);
}

现在我想设置并从具有(编辑文本和图像)的每个约束 subview 中获取数据,我知道如何访问父项的子项但我不知道如何访问 View (编辑文本和图片)每个 child 组的。

最佳答案

也许简单的方法是将 ConstraintLayout 保存在列表中,这样您可以更轻松地设置和获取数据。

关于android - 如何在动态 View 中添加/获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51801092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com