使用 EditText 动态制作布局 - Android
Dynamically made Layout with EditText - Android
这不是一个问题,更多的是解释,我想知道发生了什么,而不是像对待黑匣子一样对待事情。我正在制作一个 android 应用程序,我创建了一个表单,您可以在其中动态创建一个新字段以添加到表单中,并且该字段有 2 个彼此相邻的编辑文本。
formLayout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:id="@+id/team_form_player_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginRight="7dp"
android:layout_weight="1"
android:background="@color/white"
android:hint="@string/form_hint_team_player_name"
android:inputType="textCapWords"
android:padding="15dp"/>
<EditText
android:id="@+id/team_form_player_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@color/white"
android:hint="@string/form_hint_team_player_number"
android:inputType="number"
android:padding="15dp"/>
</LinearLayout>
点击时它位于按钮侦听器内,它将创建新字段
View layout = getLayoutInflater().inflate(R.layout.layout_add_new_field_team, null);
EditText name = (EditText)layout.findViewById(R.id.team_form_player_name);
EditText number = (EditText)layout.findViewById(R.id.team_form_player_number);
playerName.add(name); //arraylist
playerNumber.add(number); //arraylist
containerLayout.addView(layout);
由于它的布局不同,我不得不放大视图。无论如何,我的问题是这个。这到底是怎么回事?通过代码,我每次都在其尊重的数组中添加一个编辑文本。我每次都添加相同的布局,我什至没有使用 "new" 所以它没有实例化任何新的东西。如果我一次创建 10 个字段,则填写这些字段。 运行 这个方法
for(EditText edit : playerNumber) {
String test = edit.getText().toString();
System.out.println(test);
}
它为我提供了每个字段中的所有正确值。这是怎么发生的?因为在单击添加字段时,它会立即将编辑文本添加到字段为 EMPTY 的数组中。并且没有代码将值输入数组。我只是对它的工作原理感到困惑,并且想知道它是如何工作的。有人知道吗?
.inflate(layoutResId) == "create new instance of this layout"
所以基本上每次您使用 inflate 时,您都会在布局中创建所有视图的实例,在您的例子中是一个 LinearLayout 和其中的两个 EditText 视图。
这不是一个问题,更多的是解释,我想知道发生了什么,而不是像对待黑匣子一样对待事情。我正在制作一个 android 应用程序,我创建了一个表单,您可以在其中动态创建一个新字段以添加到表单中,并且该字段有 2 个彼此相邻的编辑文本。
formLayout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:id="@+id/team_form_player_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginRight="7dp"
android:layout_weight="1"
android:background="@color/white"
android:hint="@string/form_hint_team_player_name"
android:inputType="textCapWords"
android:padding="15dp"/>
<EditText
android:id="@+id/team_form_player_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@color/white"
android:hint="@string/form_hint_team_player_number"
android:inputType="number"
android:padding="15dp"/>
</LinearLayout>
点击时它位于按钮侦听器内,它将创建新字段
View layout = getLayoutInflater().inflate(R.layout.layout_add_new_field_team, null);
EditText name = (EditText)layout.findViewById(R.id.team_form_player_name);
EditText number = (EditText)layout.findViewById(R.id.team_form_player_number);
playerName.add(name); //arraylist
playerNumber.add(number); //arraylist
containerLayout.addView(layout);
由于它的布局不同,我不得不放大视图。无论如何,我的问题是这个。这到底是怎么回事?通过代码,我每次都在其尊重的数组中添加一个编辑文本。我每次都添加相同的布局,我什至没有使用 "new" 所以它没有实例化任何新的东西。如果我一次创建 10 个字段,则填写这些字段。 运行 这个方法
for(EditText edit : playerNumber) {
String test = edit.getText().toString();
System.out.println(test);
}
它为我提供了每个字段中的所有正确值。这是怎么发生的?因为在单击添加字段时,它会立即将编辑文本添加到字段为 EMPTY 的数组中。并且没有代码将值输入数组。我只是对它的工作原理感到困惑,并且想知道它是如何工作的。有人知道吗?
.inflate(layoutResId) == "create new instance of this layout"
所以基本上每次您使用 inflate 时,您都会在布局中创建所有视图的实例,在您的例子中是一个 LinearLayout 和其中的两个 EditText 视图。