[英]Get text from EditText within for loop in Kotlin
I'm developing an app in Android in Kotlin, and in one activity I have 10 editText.我正在 Kotlin 的 Android 中开发一个应用程序,并且在一项活动中我有 10 个 editText。 How i could get the text of all editTexts within a for loop?
我如何在 for 循环中获取所有 editTexts 的文本? EditTexts are into Constraint Layouts, which are into a Linear Layout.
EditTexts 属于约束布局,后者属于线性布局。 enter image description here
在此处输入图像描述
You can iterate over the child views of the parent view group and just gather the texts, something like this:您可以遍历父视图组的子视图并收集文本,如下所示:
val parentView: ViewGroup = findViewById(R.id.parent)
for (i in 0 until parentView.childCount) {
val view: View = parentView.getChildAt(i)
if (view is EditText) {
Log.d("text", view.text.toString())
}
}
Change the R.id.parent
to the correct id of course - the id of your constraint layout.当然,将
R.id.parent
更改为正确的 id - 您的约束布局的 id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.