android - Android 数据绑定(bind)库中带有字符串文字的三元运算符

标签 android android-databinding

我有一个 EditText,我想根据 Parameter 是否为数字来设置其 inputType:

interface Parameter {
    boolean isNumeric();
}

我有这个:

   <EditText
       ...
       android:inputType="@{parameter.isNumeric()? number : text}"/>

但是解析XML时出现错误。我尝试将 numbertext 值括在单引号和双引号(转义)中。

我该如何解决这个问题?

最佳答案

numbertext值在 Java 上下文中将被视为变量。您必须确保设置适当的 Java 值。首先你必须导入InputType在你的<data>堵塞。然后使用相关的整数值。

<EditText
    android:inputType="@{parameter.isNumeric() ? InputType.TYPE_CLASS_NUMBER : InputType.TYPE_CLASS_TEXT}" />

关于android - Android 数据绑定(bind)库中带有字符串文字的三元运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44392267/

相关文章:

java - Android Java Google Places GET(拉取)评论 JSONException 没有评论值(value)

安卓数据绑定(bind) : How to avoid "cannot find the KaptTask" warning

如果值为真,Android 数据绑定(bind)设置填充

android - 使用 MVVM 架构和数据绑定(bind)时如何突出显示 recyclerview 项目?

android - 在 Android 上使用全局数据的推荐方式是什么?

java - 膨胀类 ja.burhanrashid52.photoeditor.PhotoEditorView 时出错

Android IllegalArgumentException : The tag for fragment_XXX is invalid. 收到:layout-sw600dp/fragment_XXX_0

android - MainActivityBinding.inflate(getLayoutInflater()) 页面未更新

java - 如何实现通信Activity-Service

android - 使用 GridLayout 编辑屏幕外的文本