简体   繁体   中英

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. How i could get the text of all editTexts within a for loop? EditTexts are into Constraint Layouts, which are into a Linear Layout. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM