isErrorEnabled 无法正常工作 Kotlin Android Studio

问题描述 投票:0回答:1

saasHI,

我正在做一个认证登录和注册的教程,isErrorEnabled 它对我不起作用,你能帮忙吗?

教程视频第三分钟 13:00

https://www.youtube.com/watch?v=QkZi4Xi9Gy8&list=PLxPn1IRBtvq_Dq57j2HGIUCDI9xZlFLW1&index=4

代码:

RegisterActivity kotlin 类

private fun validateName(): Boolean{
        var errorMessage: String? = null
        val value: String = mBinding.nameBoxRegister.text.toString()
        if(value.isEmpty()){
            errorMessage = "Full name is required / Se requiere nombre completo"
        }

        if(errorMessage != null){
          mBinding.nameBoxRegister.apply {
             isErrorEnabled = false
              error = errorMessage

          }
        }

xml布局activity_register nameBoxRegister

 <com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/nameBoxRegister"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/name_box_register"
                android:inputType="text"
                android:paddingHorizontal="8dp"
                android:paddingVertical="12dp" />
        </com.google.android.material.textfield.TextInputLayout>

尝试在网上搜索但没有找到答案。

android kotlin android-studio boolean
1个回答
0
投票

我查看了您提供的代码,像这样更改您的 XML:

 <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:id="@+id/nameBoxRegisterTil"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/nameBoxRegister"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/name_box_register"
            android:inputType="text"
            android:paddingHorizontal="8dp"
            android:paddingVertical="12dp" />
    </com.google.android.material.textfield.TextInputLayout>

现在您可以通过以下代码在

isErrorEnabled
中使用
RegisterActivity

private fun validateName(): Boolean {
    var errorMessage: String? = null
    val value: String = mBinding.nameBoxRegister.text.toString()
    if(value.isEmpty()){
        errorMessage = "Full name is required / Se requiere nombre completo"
    }

    if(errorMessage != null){
      mBinding.nameBoxRegisterTil.apply {
         isErrorEnabled = false
          error = errorMessage

      }
    }

就是这样!您的问题现在要解决了。

© www.soinside.com 2019 - 2024. All rights reserved.