我想制作一个程序,在某些时候用户必须添加一些学校科目作为输入。我创建了输入字段(见下图),点击“添加”按钮时,会在其下面创建一个相等的字段。 “删除”按钮用于删除输入字段,但必须至少有一个输入字段。
由于我是Android开发的新手,我想知道如何做到这一点。我已经研究了一些网站,但我只能使用addView
生成单个元素,如TextViews:
val relLay = findViewById<RelativeLayout>(R.id.relLay1)
val btnAdd = findViewById<Button>(R.id.btnAdd)
btnAdd.setOnClickListener{
val tv = TextView(this)
tv.text = "This is a text view"
val params : RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
params.setMargins(10, pos, 10, 10)
pos += 50 // pos is a variable that was declared previously
rellay.addView(tv)
}
如何在组中生成这些元素?此外,在创建这样的其他字段后,当用户单击“完成”时,如何读取所有创建的字段的数据?
我的input_activity.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relLay1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/relLay2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
...
<!-- Title of the input field -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add subject"
android:layout_below="@id/table1"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true"
android:textSize="36sp"
android:id="@+id/txtSubtitle2"/>
<!-- LinearLayout that I want to be created
every time the user clickes the button Add -->
<LinearLayout
android:orientation="vertical"
android:id="@+id/linLay1"
android:layout_below="@id/txtSubtitle2"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:ems="10"
android:id="@+id/edtSubjectNam"
android:hint="Subject name"/>
<TextView
android:text="Days of the week"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:id="@+id/string3"/>
<CheckBox
android:text="Sunday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:id="@+id/checkSun"/>
<CheckBox
android:text="Monday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:id="@+id/checkMon"/>
<CheckBox
android:text="Tuesday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:id="@+id/checkTue"/>
<CheckBox
android:text="Wednesday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:id="@+id/checkWed"/>
<CheckBox
android:text="Thursday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:id="@+id/checkThu"/>
<CheckBox
android:text="Friday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:id="@+id/checkFri"/>
<CheckBox
android:text="Saturday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:id="@+id/checkSat"/>
<!-- LinearLayout of the buttons Done, Remove and Add -->
<LinearLayout
android:orientation="horizontal"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDone"
android:layout_weight="1"/>
<Button
android:text="Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnAdd"
android:layout_weight="1"/>
<Button
android:text="Remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnRem"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
我在Kotlin(InputActivity.kt
)的代码仍然是空的,因为我做不了多少。任何帮助都非常非常受欢迎。我还想从“删除”按钮代码看起来有些帮助,但我最关心的是“添加”按钮。
所以你想做的就是用你的视图制作你自己的布局文件。然后,您将为视图充气并将其添加到父视图中
public View appendView(Activity activity) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.NAME_OF_YOUR_LAYOUT_FILE, null);
LinearLayout parentLayout = activity.findViewById(R.id.YOUR_CONTAINER);
parentLayout.addView(view);
}
删除只是相同但不是addView你会删除视图。可能想为淡出添加动画或将视图设置为animateLayout
要读取数据,只需执行view.findViewById(R.id.FIELD),然后从中获取相关值并保存。可能想为它上课