简体   繁体   English

为什么Android中的这种简单数据绑定无法正常工作?

[英]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: 我正在尝试根据此问题中指出的Khemraj的建议使用数据绑定:

Enable and disable Button according to the text in EditText in Android 根据Android中EditText中的文本启用和禁用Button

To enable a button based on the value of other fields using xml only. 仅使用xml启用基于其他字段值的按钮。

The problem is that any expression I use in android: enabled =" @ {...} " it is ignored. 问题是我在android: enabled =" @ {...} "使用的任何表达式android: enabled =" @ {...} "都会被忽略。 I also tried to insert a single Boolean value directly to false but the button remains active. 我还尝试将单个布尔值直接插入false,但该按钮保持活动状态。

Thanks in advance for the help. 先谢谢您的帮助。

These are the current configurations of my project: 这些是我项目的当前配置:

Activity XML: 活动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): Build.gradle(模块应用程序):

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 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 您在xml文件中缺少数据标签,请尝试添加它

<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. 我必须在活动的onCreate中使用此代码来激活没有标签数据的字段之间的数据绑定。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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