Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么我使用setOnFocusChangeListener进行的验证不';不行吗?_Android_Validation_Kotlin - Fatal编程技术网

Android 为什么我使用setOnFocusChangeListener进行的验证不';不行吗?

Android 为什么我使用setOnFocusChangeListener进行的验证不';不行吗?,android,validation,kotlin,Android,Validation,Kotlin,我想验证一下,每当用户将TextInputLayout留空并尝试单击其他位置时,就会出现一个错误,如“此字段不能为空”。 我试着这样做,但它不起作用: fun TextInputLayout.emptyItem() { this.editText?.setOnFocusChangeListener { _, hasFocus -> if (!hasFocus) { if (this.editText?.text?.trim().isNu

我想验证一下,每当用户将TextInputLayout留空并尝试单击其他位置时,就会出现一个错误,如“此字段不能为空”。 我试着这样做,但它不起作用:

   fun TextInputLayout.emptyItem() {
    this.editText?.setOnFocusChangeListener { _, hasFocus ->
        if (!hasFocus) {
            if (this.editText?.text?.trim().isNullOrEmpty()) {
                putError(context.getString(R.string.error_field_obrigatory_message))
            } else {
                this.error = null
            }
        }
    }
这是我的xml:

 <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/text_Input_Valor"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="57dp"
                    android:hint="@string/credito_inform_the_value"
                    android:theme="@style/TextInputLayoutTheme.ClearButton"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    app:errorEnabled="true"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/valor"
                        style="@style/TextInputEditTextTheme"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:imeOptions="actionDone"
                        android:inputType="numberDecimal"
                        android:textSize="@dimen/text_medium" />

                </com.google.android.material.textfield.TextInputLayout>

您应该使用
资源
活动
实例检索字符串资源。
尝试更改您的
emptyItem
代码,如下所示:

fun TextInputLayout.emptyItem() {
    this.editText?.setOnFocusChangeListener { _, hasFocus ->
        if (!hasFocus) {
            val stringValue = this.editText?.text?.trim() ?: ""
            if (stringValue == "") {
                putError(activity.resources.getString(R.string.error_field_obrigatory_message))
            } else {
                this.error = null
            }
        }
    }

把错误放在method@javdromero完成
fun TextInputLayout.emptyItem() {
    this.editText?.setOnFocusChangeListener { _, hasFocus ->
        if (!hasFocus) {
            val stringValue = this.editText?.text?.trim() ?: ""
            if (stringValue == "") {
                putError(activity.resources.getString(R.string.error_field_obrigatory_message))
            } else {
                this.error = null
            }
        }
    }