代码之家  ›  专栏  ›  技术社区  ›  Janderson Almeida

Ilhasoft+Kotlin在数据绑定验证器中出错

  •  1
  • Janderson Almeida  · 技术社区  · 7 年前

    我正在尝试使用Android库 "Data Binding Validator by Ilhasoft" 使用Kotlin,但我得到了一个错误:未解析引用:验证。

    这是我的根构建。格拉德尔:

    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
    }
    

    这是我的应用程序版本。格拉德尔:

    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\u地籍用户:

    <?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>
    

    最后是我片段中的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.

    有人能帮我吗。 谢谢

    1 回复  |  直到 7 年前
        1
  •  1
  •   kirtan403    7 年前

    validate 是库示例中按钮的id。

    您应该使用要在其上验证的按钮的id。我想是的 new_user_button .

    所以对于绑定,应该是这样的 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
    }