android - 双向数据绑定(bind) : View is missing user defined type

标签 android android-layout data-binding android-view 2-way-object-databinding

今天我发现了最近推出的 two-way data binding Android Studio 预览中的功能,并决定试一试。

我有一个非常简单的布局(下面的代码),用于撰写和发送消息。我试图实现的是在字段中没有输入文本时使用按钮“disabled”(并且在未来,相应地使用一些不同的图像)。

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
<data>
    <variable name="msg" type="String"/>
</data>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/new_message_input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingStart="10dp"
        android:hint="@string/hint_compose_message"
        android:inputType="textAutoCorrect|textMultiLine"
        android:text="@={msg}"/>
    <ImageButton
        android:id="@+id/btn_send_message"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:src="@drawable/ic_send"
        android:enabled="@{!new_message_input.text.isEmpty()}"
        android:clickable="@{!new_message_input.text.isEmpty()}"/>
</LinearLayout>
</layout>

第一个链接中的示例代码表明,像这样应该就足够了:

<layout ...>
  <data>
    <import type="android.view.View"/>
  </data>
  <RelativeLayout ...>
    <CheckBox android:id="@+id/seeAds" .../>
    <ImageView android:visibility="@{seeAds.checked ? View.VISIBLE : View.GONE}" .../>
  </RelativeLayout>
</layout>

但是,当尝试为 ImageButtonenabled/clickable 属性实现类似的逻辑时,出现以下错误:

Error:java.lang.RuntimeException: java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Identifiers must have user defined types from the XML file. new_message_input is missing it

问题肯定出在这两行上,因为删除它们可以正确创建绑定(bind)类。

我的问题是:

  • 我做错了什么?
  • 我该如何解决这个问题?

我也尝试了一些不同的做法,但结果是一样的:

<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
    <import type="android.widget.EditText"/>
    ...
</data>
<LinearLayout
  ...
  <ImageButton
    ...
    android:enabled="@{!(((EditText)new_message_input).getText().toString().isEmpty())}"
    android:clickable="@{!(((EditText)new_message_input).getText().toString().isEmpty())}"/>

最佳答案

多哈。

我忘记了数据绑定(bind)过程将 XML ID 转换为绑定(bind)类中的属性,所有这些都是用小驼峰式编写的。这意味着为了使用数据绑定(bind)从 "@id/btn_send_message" 中引用 "@id/new_message_input",我应该使用生成的名称,在本例中为 newMessageInput

这在示例中不是很明显,因为它包含一个 View,其 @id 已经在 camelCase 中,因此与生成的名称 - 从而自动工作。

因此,解决方案是替换这些行:

    android:enabled="@{!new_message_input.text.isEmpty()}"
    android:clickable="@{!new_message_input.text.isEmpty()}"/>

与:

    android:enabled="@{!newMessageInput.text.isEmpty()}"
    android:clickable="@{!newMessageInput.text.isEmpty()}"/>

或者我可以完全解决这个问题:

    android:enabled="@{!msg.isEmpty()}"
    android:clickable="@{!msg.isEmpty()}"/>

旁注:

如果 EditText 最初是空的(因此我们希望按钮被禁用),我们应该将一个空的 String 对象附加到 View (通过 Java;例如 StringUtils.EMPTY),在布局膨胀时,正确地使按钮不可点击。

关于android - 双向数据绑定(bind) : View is missing user defined type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36229529/

相关文章:

java - Android 初学者 : Ads at the Bottom of the XML

android - 设置 Listview 的项目高度不起作用

android - 如何均衡按钮之间的空间?

c# - 使用 WPF MVVM Light DispatcherHelper

data-binding - Grails 数据绑定(bind)疑问

c# - 绑定(bind)到 IList,但得到 "Complex DataBinding accepts as a data source either an IList or an IListSource"

android - 如何通过其他平台共享图像

android - 如何在不从 Azure 下载 Blob 的情况下查看它

Android - 出于测试目的将堆大小限制为 16MB?

java - 如何在 MPAndroidChart 中的条形图上正确添加标签