【问题标题】:Why this simple data-binding in Android not working?为什么Android中这个简单的数据绑定不起作用?
【发布时间】:2019-07-29 08:57:03
【问题描述】:

我正在尝试根据 Khemraj 在此问题中的建议使用数据绑定:

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

仅使用 xml 根据其他字段的值启用按钮。

问题是我在android: enabled =" @ {...} " 中使用的任何表达式 它被忽略了。我还尝试将单个布尔值直接插入 false,但按钮仍然处于活动状态。

提前感谢您的帮助。

这些是我项目的当前配置:

活动 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(模块应用):

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

【问题讨论】:

    标签: android xml data-binding expression


    【解决方案1】:

    您的 xml 文件中缺少数据标签,请尝试添加它

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

    【讨论】:

    • 在 Khemraj 的问题链接中说字段的 id 已经足够了,但对我来说还不够。对吗?
    • &lt;data&gt; 标签是数据绑定的必要条件,我认为 Khemraj 假设每个人都已经知道这一点
    【解决方案2】:

    尤里卡! 我必须在我的活动的 onCreate 中使用此代码来激活没有标签数据的字段之间的数据绑定。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-26
      • 2014-05-31
      • 1970-01-01
      • 2010-10-02
      • 2012-07-05
      • 1970-01-01
      • 2011-04-09
      相关资源
      最近更新 更多