简体   繁体   中英

Why this simple data-binding in Android not working?

I'm trying to use data binding according to Khemraj's suggestion indicated in this question:

Enable and disable Button according to the text in EditText in Android

To enable a button based on the value of other fields using xml only.

The problem is that any expression I use in android: enabled =" @ {...} " it is ignored. I also tried to insert a single Boolean value directly to false but the button remains active.

Thanks in advance for the help.

These are the current configurations of my project:

Activity XML:

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="...">

        <EditText
            android:id="@+id/etTarga"
            ...

        <EditText
            android:id="@+id/etModello"
            ...

        <Button
            ...
            android:enabled="@{false}"
            ...
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Build.gradle (Module app):

apply plugin: 'com.android.application'

android {
        compileSdkVersion 29
        buildToolsVersion "29.0.1"
        defaultConfig {
          applicationId "..."
          minSdkVersion 15
          targetSdkVersion 29
          versionCode 1
          versionName "1.0"
          testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
               minifyEnabled false
               proguardFiles getDefaultProguardFile('proguard-android- 
               optimize.txt'), 'proguard-rules.pro'
           }
        }
        compileOptions {
            sourceCompatibility = '1.8'
            targetCompatibility = '1.8'
        }
        dataBinding {
           enabled = true
        }
    }

    dependencies {
       implementation fileTree(dir: 'libs', include: ['*.jar'])
       implementation 'androidx.appcompat:appcompat:1.0.2'
       implementation 'com.google.android.material:material:1.0.0'
       implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
       implementation 'androidx.annotation:annotation:1.0.1'
       implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
       testImplementation 'junit:junit:4.12'
       androidTestImplementation 'androidx.test:runner:1.2.0'
       androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
   }

gradle.properties

org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
android.enableJetifier=true
android.databinding.enableV2=true

You are missing data tag in your xml file, try adding it

<data>
       <variable name="something" type="something"/>
</data>

Eureka ! I had to use this code in onCreate of my activity for activate data binding between fields without tag data.

MySimpleActivityBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_my_simple);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM