android - Ilhasoft + Kotlin 的数据绑定(bind)验证器错误

标签 android validation kotlin android-databinding

我正在尝试使用 Android 库 "Data Binding Validator by Ilhasoft"使用 Kotlin,但我收到此错误: Unresolved reference :验证。

那是我的根build.gradle:

buildscript {
    ext.kotlin_version = '1.1.60'
    ext.android_plugin_version = '3.0.0'
    ext.data_binding_validator_verson = '1.0.0'
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"  

    }
}

allprojects {
    repositories {
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

那是我的APP build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.0"
    defaultConfig {
        applicationId "com.main.janderson.meuapp"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        vectorDrawables.useSupportLibrary = true

        multiDexEnabled true

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

     dataBinding {
        enabled = true
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })  

    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    kapt "com.android.databinding:compiler:$android_plugin_version"
    compile "com.github.Ilhasoft:data-binding-validator:$data_binding_validator_verson"
}
repositories {
    mavenCentral()
    google()
}

kapt {
    generateStubs = true
}

那是我的 fragment_cadastrar_user:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="user"
            type="com.main.janderson.meuapp.model.User" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <ProgressBar
            android:id="@+id/cadastrar_user_progress"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:visibility="gone" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/cadastrar_form"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/first_name_user"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/first_name"
                        android:inputType="textCapSentences"
                        android:lines="1"
                        android:maxLength="30"
                        android:maxLines="1"
                        android:text="@{user.firstName}"
                        app:validateEmpty="@{false}" />

                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/last_name_user"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/last_name"
                        android:inputType="textCapSentences"
                        android:lines="1"
                        android:maxLength="30"
                        android:maxLines="1"
                        android:text="@{user.lastName}"
                        app:validateEmpty="@{false}" />

                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/user_email"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/prompt_email"
                        android:inputType="textEmailAddress"
                        android:lines="1"
                        android:maxLength="100"
                        android:maxLines="1"
                        android:text="@{user.email}"
                        app:validateType='@{"email"}' />

                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/user_phone"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/phone"
                        android:inputType="phone"
                        android:lines="1"
                        android:maxLength="20"
                        android:maxLines="1"
                        android:text="@{user.phone}" />

                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/user_pass"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/prompt_password"
                        android:inputType="textPassword"
                        android:lines="1"
                        android:maxLength="20"
                        android:maxLines="1"
                        android:text="@{user.password}"
                        app:validateMinLength="@{4}" />

                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/user_confir_pass"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/prompt_repeat_password"
                        android:imeActionLabel="@string/cadastrar"
                        android:imeOptions="actionUnspecified"
                        android:inputType="textPassword"
                        android:lines="1"
                        android:maxLength="20"
                        android:maxLines="1" />

                </android.support.design.widget.TextInputLayout>

                <Button
                    android:id="@+id/new_user_button"
                    style="?android:textAppearanceSmall"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:text="@string/cadastrar"
                    android:textStyle="bold" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
</layout>

最后是我的 fragment 中的 onCreateView:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    val view = inflater.inflate(R.layout.fragment_cadastrar_user, null)

    //Binding Api
    val binding: FragmentCadastrarUserBinding = DataBindingUtil.setContentView(activity as Activity, R.layout.fragment_cadastrar_user)
    val  validator =  Validator(binding);

    binding.validate.setOnClickListener(View.OnClickListener {
        if (validator.validate()) {
            registerUser()
        }
    })

    binding.setUser(user);

    view.user_phone.addTextChangedListener(PhoneNumberFormattingTextWatcher())

    return view
}

And to complement this is a picture with my error.

有人可以帮我解决这个问题吗? 谢谢!

最佳答案

validate是库例子中按钮的id。

您应该使用要验证的按钮的 ID。我认为它是 new_user_button

所以有了绑定(bind),它应该像newUserButton

因此您的完整示例如下:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    val view = inflater.inflate(R.layout.fragment_cadastrar_user, null)

    //Binding Api
    val binding: FragmentCadastrarUserBinding = DataBindingUtil.setContentView(activity as Activity, R.layout.fragment_cadastrar_user)
    val  validator =  Validator(binding);

    // CHANGE IN BELOW LINE
    binding.newUserButton.setOnClickListener(View.OnClickListener {
        if (validator.validate()) {
            registerUser()
        }
    })

    binding.setUser(user);

    view.user_phone.addTextChangedListener(PhoneNumberFormattingTextWatcher())

    return view
}

关于android - Ilhasoft + Kotlin 的数据绑定(bind)验证器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47369053/

相关文章:

Java/Kotlin/Spring Boot。如何在异常发生时自动获取参数值?

java - Kotlin 中的错误 "must not be null"

java - 在android中使用 Intent 与多个图像共享文本

android-4.0-ice-cream-sandwich - Novo 7 Paladin 平板电脑 (ICS 4.01) 上的 INSTALL_FAILED_INVALID_APK 错误

android - 构建android项目产生make error 2

一组规则的正则表达式密码验证

android - 如何创建具有自定义 ID 值的微调器?

java - Android 异步任务中的返回列表

javascript - 解决正则表达式问题;验证js

validation - 在 Liberator 中使用 Prismatic/schema 进行表单验证