首页 > 解决方案 > 有没有办法或内置方法从android材料TextInputLayout(om.google.android.material.textfield.TextInputLayout)获取文本?

问题描述

我曾尝试使用 getText() 但这在使用该文本字段时不可用,下面是文本字段:

        <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/firstnametxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="120dp"
        android:layout_marginRight="25dp"
        android:hint="First Name">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text" />
    </com.google.android.material.textfield.TextInputLayout>

标签: androidmaterial-design

解决方案


TextInputLayout有一个editText字段,您可以将其转换为您的TextInputEditText并使用它的text字段获取其中的文本。例如在 Kotlin 中它看起来像

val editText = textInputLayout.editText as TextInputEditText
editText.text

推荐阅读