我的应用程序是关于创建调查的。为此,用户应该能够动态地创建新字段。
因此,在创建新调查时,仅显示 2 个按钮(添加和删除)的活动。当用户单击添加按钮时,会出现一行,其中包含一个 EditText(以便他可以编写问题)、一个 Spinner(使用户能够选择字段类型:文本、RadioButton、CheckBox 等)和另一个 EditText (如果适用,其中写有不同的可用答案选项)。
为了实现这一点,我创建了一个包含这些小部件的 XML 文件,并且每次用户单击添加按钮时,XML 都会膨胀。它在我第一次按下按钮时起作用,但在那之后......什么也没有发生。
我在某处读到,除非在循环中完成,否则您不能在父母中为同一个孩子充气 2 次。
所以,我的问题是:
-> 我怎样才能解决这个问题并获得预期的效果?
-> 为什么这不适用于循环?
任何帮助或提示表示赞赏。再次感谢社区给予我的所有支持。
注意:在 android 文档中,他们谈到了克隆充气机,但那里的信息很少,我找不到任何其他信息。
源代码:
XML - 主容器:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/mds_new_addfield_button"
android:tag="new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Field"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
/>
<Button
android:id="@+id/mds_new_deletefield_button"
android:tag="delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Field"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
XML - 膨胀:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow>
<TextView
android:id="@+id/mdsnew_field_tv"
style="@style/dsn_field"
android:text="Field Name "
/>
<TextView
android:id="@+id/mdsnew_type_tv"
style="@style/dsn_field"
android:text="Type of Field "
/>
<TextView
android:id="@+id/mdsnew_choices_tv"
style="@style/dsn_field"
android:text="Choices"
/>
</TableRow>
<TableRow>
<EditText
android:id="@+id/mdsnew_fieldname_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Field Name"
android:textSize="18sp"
/>
<Spinner
android:id="@+id/mdsnew_type_sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/mdsnew_choices_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choices "
android:textSize="18sp"
/>
</TableRow>
</TableLayout>
添加按钮:
LinearLayout myRoot = (LinearLayout) findViewById(R.id.wrapper);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.mds_new_row, myRoot);