Android - 如何使用数据绑定在 Button/TextView 中设置自己的文本

Android - How to set own text in Button/TextView using data-binding

我刚开始使用 android data-binding。我想根据用户登录状态更改按钮文本,但出现以下错误。

日志显示语法错误,已尝试但未发现任何问题。
代码

Logcat错误

Error:|', '?', ':', '??'} Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Syntax error: mismatched input 'gin' expecting {'.', '::', '[', '+', '-', '*', '/', '%', '<<', '>>>', '>>', '<=', '>=', '>', '<', 'instanceof', '==', '!=', '&', '^', '|', '&&', '||', '?', ':', '??'} file:C:\Users\user\AndroidStudioProjects\Test\app\src\main\res\layout\binding_activity.xml loc:30:28 - 30:61 ****\ data binding error ****

如有任何帮助,我们将不胜感激..

你的符号可能应该是这样的:

user.isLogged ? @string/logout : @string/login

正如Sherlock所说"You see, but you do not observe. The distinction is clear"

而我所做的是,我没有遵守官方文件。你也可以这样设置文字

When using single quotes around the attribute value, it is easy to use double quotes in the expression:

  android:text='@{user.isLogged ? "Logout" : "Login"}'

It is also possible to use double quotes to surround the attribute value. When doing so, String literals should either use the quote or back quote (`).

 android:text="@{user.isLogged ? `Logout` : `Login`}" //text is inside back-quote

Data Binding Guide- String Literals