当我们不想重复XML代码时,就会创建动态视图。在本文中,我们将创建一个单独的布局,并在LinearLayout中填充它们,然后将用户数据存储在ArrayList中,然后将其显示为Toast。下面提供了一个示例视频,以使您对我们在本文中将要做的事情有个大概的了解。请注意,我们将使用Kotlin语言实施此项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言
步骤2:在build.gradle(app)中添加依赖项
在build.gradle(app)中添加View Binding依赖项,然后单击立即同步按钮。
android {
..
buildFeatures {
viewBinding true
}
}
步骤3:使用activity_main.xml文件
转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。 请注意,它具有ID为parent_linear_layout的线性布局,我们将对其进行膨胀以添加视图。
XML
XML
XML
- Exp
- 1 Year
- 2 Year
- 3 Year
- 4 Year
Kotlin
class Language(
var name: String = "",
var exp: String = ""
) {
}
Kotlin
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.Spinner
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
// Provide your package name here
import com.example.addviewsdynamically.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
// initiate viewBinding
private var _binding: ActivityMainBinding? = null
private val binding get() = _binding!!
// create an arraylist in which
// we will store user data
private var languageList = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// This addButton is used to add a new view
// in the parent linear layout
binding.buttonAdd.setOnClickListener {
addNewView()
}
// This Submit Button is used to store all the
// data entered by user in arraylist
binding.buttonSubmitList.setOnClickListener {
saveData()
}
// This Show button is used to show data
// stored in the arraylist.
binding.buttonShowList.setOnClickListener {
showData()
}
}
// This function is called after
// user clicks on addButton
private fun addNewView() {
// this method inflates the single item layout
// inside the parent linear layout
val inflater = LayoutInflater.from(this).inflate(R.layout.row_add_language, null)
binding.parentLinearLayout.addView(inflater, binding.parentLinearLayout.childCount)
}
// This function is called after user
// clicks on Submit Button
private fun saveData() {
languageList.clear()
// this cunts the no of child layout
// inside the parent Linear layout
val count = binding.parentLinearLayout.childCount
var v: View?
for (i in 0 until count) {
v = binding.parentLinearLayout.getChildAt(i)
val languageName: EditText = v.findViewById(R.id.et_name)
val experience: Spinner = v.findViewById(R.id.exp_spinner)
// create an object of Language class
val language = Language()
language.name = languageName.text.toString()
language.exp = experience.selectedItem as String
// add the data to arraylist
languageList.add(language)
}
}
// This function is called after user
// clicks on Show List data button
private fun showData() {
val count = binding.parentLinearLayout.childCount
for (i in 0 until count) {
Toast.makeText(this, "Language at $i is ${languageList[i].name} and experience is ${languageList[i].exp} ", Toast.LENGTH_SHORT).show()
}
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
步骤4:建立新的版面配置
创建一个名为row_add_language.xml的新布局。这是我们将在id为parent_linear_layout的线性布局中填充的单独布局。
XML格式
第5步:在Spinner中使用的字符串.xml中添加条目项
XML格式
- Exp
- 1 Year
- 2 Year
- 3 Year
- 4 Year
步骤6:建立新的Kotlin类别
创建一个新的Kotlin类Language.kt 。这是通用类,我们将使用Arraylist来存储数据。
科特林
class Language(
var name: String = "",
var exp: String = ""
) {
}
步骤7:使用MainActivity.kt文件
转到MainActivity.kt文件,并参考以下代码。下面是MainActivity.kt文件的代码。在代码内部添加了注释,以更详细地了解代码。
科特林
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.Spinner
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
// Provide your package name here
import com.example.addviewsdynamically.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
// initiate viewBinding
private var _binding: ActivityMainBinding? = null
private val binding get() = _binding!!
// create an arraylist in which
// we will store user data
private var languageList = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// This addButton is used to add a new view
// in the parent linear layout
binding.buttonAdd.setOnClickListener {
addNewView()
}
// This Submit Button is used to store all the
// data entered by user in arraylist
binding.buttonSubmitList.setOnClickListener {
saveData()
}
// This Show button is used to show data
// stored in the arraylist.
binding.buttonShowList.setOnClickListener {
showData()
}
}
// This function is called after
// user clicks on addButton
private fun addNewView() {
// this method inflates the single item layout
// inside the parent linear layout
val inflater = LayoutInflater.from(this).inflate(R.layout.row_add_language, null)
binding.parentLinearLayout.addView(inflater, binding.parentLinearLayout.childCount)
}
// This function is called after user
// clicks on Submit Button
private fun saveData() {
languageList.clear()
// this cunts the no of child layout
// inside the parent Linear layout
val count = binding.parentLinearLayout.childCount
var v: View?
for (i in 0 until count) {
v = binding.parentLinearLayout.getChildAt(i)
val languageName: EditText = v.findViewById(R.id.et_name)
val experience: Spinner = v.findViewById(R.id.exp_spinner)
// create an object of Language class
val language = Language()
language.name = languageName.text.toString()
language.exp = experience.selectedItem as String
// add the data to arraylist
languageList.add(language)
}
}
// This function is called after user
// clicks on Show List data button
private fun showData() {
val count = binding.parentLinearLayout.childCount
for (i in 0 until count) {
Toast.makeText(this, "Language at $i is ${languageList[i].name} and experience is ${languageList[i].exp} ", Toast.LENGTH_SHORT).show()
}
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
输出:
Github仓库在这里。