大佬教程收集整理的这篇文章主要介绍了android – 双向数据绑定:视图缺少用户定义的类型,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_8@
我有一个非常简单的布局(下面的代码),用于撰写和发送消息.我想要实现的是当字段中没有输入文本时,按钮“disabled”(以及将来会有相应的图像).@H_301_8@
@H_301_8@
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://scheR_393_11845@as.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>
第一个链接中的示例代码显示这样的内容应该足够了:@H_301_8@
@H_301_8@
<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>
但是,当尝试为ImageButton的enabled / clickable属性实现类似的逻辑时,我收到以下错误:@H_301_8@
@H_301_8@
问题肯定在于这两行,因为删除它们允许正确创建绑定类.@H_301_8@
我的问题是:@H_301_8@
我也尝试过不同的做法,但结果是一样的:@H_301_8@
@H_301_8@
<?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())}"/>
@H_301_8@
我不记得Data Binding进程将XML ID转换为Binding类中的属性,所有这些都是用较低的camel情况编写的.这意味着为了使用数据绑定从“@ id / btn_send_message”中引用“@ id / new_message_input”,@R_165_10675@用生成的名称,在本例中是newmessageInput.@H_301_8@
这个例子并不是很明显,因为它包含一个View,其@id已经存在于camelCase中,因此与生成的名称相同 – 因此可以自动运行.@H_301_8@
@H_301_8@
android:enabled="@{!new_message_input.text.isEmpty()}" android:clickable="@{!new_message_input.text.isEmpty()}"/>
有:@H_301_8@
@H_301_8@
android:enabled="@{!newmessageInput.text.isEmpty()}" android:clickable="@{!newmessageInput.text.isEmpty()}"/>
或者,如果我这样做,我可以完全解决这个问题:@H_301_8@
@H_301_8@
android:enabled="@{!msg.isEmpty()}" android:clickable="@{!msg.isEmpty()}"/>
在旁注:@H_301_8@
如果EditText最初是空的(因此我们希望禁用该按钮),我们应该在布局膨胀时将一个空的String对象附加到视图(通过Java;例如StringUtils.EMPTY),以正确地使按钮不可点击.@H_301_8@
以上是大佬教程为你收集整理的android – 双向数据绑定:视图缺少用户定义的类型全部内容,希望文章能够帮你解决android – 双向数据绑定:视图缺少用户定义的类型所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。