android-databinding 相关问题

数据绑定库用于编写声明性布局并最小化绑定应用程序逻辑和布局所需的粘合代码。数据绑定库提供了灵活性和广泛的兼容性 - 它是一个支持库,因此您可以将它与所有Android平台版本一起使用,返回到Android 2.1(API级别7+)。

数据绑定:如果属性不为空则设置属性

无法理解...如何仅在变量字段不为空时设置视图的某些属性? 例如 无法理解...如何仅在变量字段不为空时设置视图的某些属性? 例如 <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="item" type="com.test.app.Item" /> </data> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal"> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:layout_margin="16dp" android:src="@{item.getDrawable()}"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginEnd="16dp" android:layout_marginLeft="72dp" android:layout_marginRight="16dp" android:layout_marginStart="72dp" android:layout_toLeftOf="@id/action" android:layout_toStartOf="@id/action" android:orientation="vertical"> <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:textColor="@color/black_87" android:textSize="16sp" android:text="@{item.getTitle()}"/> <TextView android:id="@+id/text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoLink="web|email" android:linksClickable="false" android:singleLine="true" android:textColor="@color/black_54" android:textSize="14sp" android:text="@{item.getSubtitle()}"/> </LinearLayout> </RelativeLayout> </layout> Item 的某些字段可以为 null,不必要时我不会调用布局视图的方法。我不会得到NullPointerException。仅当属性不为空时,如何设置属性? 附注对不起英语。 数据绑定通常通过检查 NullPointerException 并分配默认值(例如 null)来避免 NullPointerException,即使 item 本身在示例中为 null。 但是对项目属性进行 null 检查的基本示例: android:text='@{item.title != null ? user.title : ""}' 或者使用“空合并运算符”。空合并运算符 (??) 如果左操作数不为空,则选择左操作数;如果为空,则选择右操作数。 android:text='@{item.title ?? ""}' 请注意,title 或 getTitle 并不重要。 数据绑定不需要检查null值,它会被处理 通过绑定类。 如果您需要检查 null 用于其他目的(例如设置默认值),那么您可以这样使用。 android:text='@{item.gender != null ? item.gender : @string/male}' 或 android:text='@{item.gender ?? @string/male}' 上面两个例子都是一样的。这里 @string/male 为默认值,当 item.gender 为 null 时。 我尝试过这样的事情。 <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <import type="android.text.TextUtils"/> <variable name="mOrder" type="com.test.app.models.Order" /> </data> <TextView android:id="@+id/mLblSource" android:layout_width="100dp" android:layout_height="40dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:text='@{TextUtils.isEmpty(mOrder.order_id) ? "- -" : mOrder.order_id}' app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> </layout> android:visibility="@{UserDetails == null ? View.INVISIBLE : View.VISIBLE}"

回答 4 投票 0

通过数据绑定,如何将函数(或 lambda)变量传递给包含的布局

我将设置编辑器操作为edittext。 我的第一个代码是 @BindingAdapter(“onEditorAction”) 有趣的bindOnEditorAction(视图:TextView,事件:()->单位){ view.setOnEditorActionListener ...

回答 3 投票 0

错误:找不到符号View root = inflater.inflate(R.layout.toolbar,parent,false);

我使用数据绑定来连接UI,但是当我重建项目时,出现以下错误: 错误:找不到符号 View root = inflater.inflate(R.layout.toolbar, Parent, false); ...

回答 1 投票 0

如何在Android Studio 3.0.0中使用数据绑定和Kotlin

我刚刚开始使用 Android Studio 3.0.0,但是每次我尝试构建我的项目时都会收到此错误: 错误:以下任务之间的循环依赖关系: :app:compileDebugKotlin +--- :应用程序:

回答 5 投票 0

androidx.databinding.ViewDataBinding.IncludedLayouts 在 ViewDataBinding 中具有受保护的访问权限

这是 Android Studio 中的一个工作项目,今天当我打开项目时,它显示多个 BindingImpl 类的以下错误。每个 BindingImpl 类都有两种类型的错误。 该...

回答 7 投票 0

无法解析“DashboardActivity”中的“inflate”方法

这是一个简单的应用程序。我正在使用 Intent 从 Activity_main.xml 导航到 Activity_dashboard.xml 在 DashboardActivity.java 文件中,我尝试使用数据绑定属性。但我正在...

回答 1 投票 0

databinding.ActivityMainBinding无法在Android项目中导入

我有一个正在运行的 Android Studio 项目,也通过 USB 安装在我的智能手机上,它是一个简单的基本计算器。 现在我必须重写我教授的课程的代码,但 Android Studio 否认了这一点

回答 5 投票 0

类 NumberpickercustomlayoutBinding 是公共的,应在名为 NumberpickercustomlayoutBinding.java 的文件中声明

我已经在我的android studio项目中启用了数据绑定。每当我构建项目时,它都会显示此错误: “错误:类 NumberpickercustomlayoutBinding 是公共的,应该在文件中声明

回答 1 投票 0

如何使用View Binding代替butterknife?

我正在更新一个应用程序。在新程序中,我打算使用视图绑定,因为 butterknife 库不起作用。我的代码如下。我该如何使用新方法? 前面的代码使用

回答 1 投票 0

Android 数据绑定生成的代码中缺少 return 语句

@BindingAdapter( “onTimeViewClick”, “onLocationViewClick”, "onSetDeliveryAddressClick", ) 内部乐趣 setSubHeaderData( onTimeViewClick: (() -> Uni...

回答 1 投票 0

如何修复 Android Studio 错误“无法访问类...检查模块类路径是否缺少或冲突的依赖项”

我正在练习如何使用 SafeArgs 和其他一些 kotlin 工具。 即使添加了 SafeArgs 插件和类路径后,我仍然收到一条错误消息: 无法访问类'com.example.testingtaskmanage...

回答 2 投票 0

当我快速重新打开之前的片段时,getBinding 返回 null (NullPointerException)

我有 getBinding 问题。我的 BaseFragment 实现在这里: 私有变量_绑定:T? = 空 protected val 绑定 get() = _binding!! 重写 fun onCreateView( 充气机:布局中...

回答 1 投票 0

TextInputEditText 在带有数据绑定的输入上冻结

我正在使用带有数据绑定的 TextInputEditText - 直到最近它都运行良好。这是出现此问题的布局之一: 我正在使用 TextInputEditText 和 databinding - 直到最近它都运行良好。这是出现此问题的布局之一: <android.support.design.widget.TextInputLayout android:id="@+id/new_pass_confirm" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="@string/new_pass_confirm_hint" app:errorEnabled="true" app:passwordToggleEnabled="true"> <android.support.design.widget.TextInputEditText android:id="@+id/new_pass_confirm_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:enabled="@{viewmodel.enabled &amp;&amp; !viewmodel.progress}" android:imeOptions="actionDone" android:inputType="textPassword" android:maxLines="1" android:text="@={viewmodel.newPassConfirm}" android:textSize="18sp" /> </android.support.design.widget.TextInputLayout> 但是现在,每当用户尝试输入任何内容时,应用程序都会冻结,并且 logcat 一遍又一遍地充斥着重复的消息: V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@ccaf106 nm : package.my ic=com.android.internal.widget.EditableInputConnection@87ca3c7 I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@e26ddb nm : package.my ic=com.android.internal.widget.EditableInputConnection@43b078 I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@4fff78e nm : package.my ic=com.android.internal.widget.EditableInputConnection@43ddbaf I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus W/IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection 如果删除,问题就会消失 android:text="@={viewmodel.newPassConfirm}" 看起来问题出在 val newPassConfirm = ObservableField("") 尝试无限循环中更改 xml 字段 直到几天前还运行良好 我尝试过的: 删除android:imeOptions="actionDone" 删除android:inputType="textPassword" 从视图层中删除了以下各项: binding.newPassConfirm.setOnClickListener(v -> binding.newPassConfirm.setError(null)); binding.newPassConfirmEt.addTextChangedListener(new DefaultTextWatcher() { @Override public void afterTextChanged(Editable s) { binding.newPassConfirm.setError(null); } }); binding.newPassConfirmEt.setOnEditorActionListener( (v, actionId, event) -> { if (actionId == EditorInfo.IME_ACTION_DONE || event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { mViewModel.onOkClick(); return true; } return false; }); 错误原因是DataBindingAdapter中的这个方法 @BindingAdapter("android:text") public static void setText(TextView textView, String text) { textView.setText(text != null ? text : ""); } 它可能会使用数据绑定代码循环自身,从而导致大量滞后 在 XML 中添加 inputType="textPhonetic" 解决了我的问题: android:inputType="textPhonetic" 不确定哪种输入类型会导致此延迟。浪费了我一天的时间! 我刚刚和 Ildar Ishalin 有同样的情况。 我是这样解决的: @BindingAdapter("android:text") @JvmStatic fun setText(target: TextView, text: String?) { if (!target.text.contentEquals(text)) { target.text = text } }

回答 3 投票 0

java转kotlin后获取包不存在错误

将 java 项目转换为 kotlin 后,我收到这些错误。之前工作得很好。 我在我的项目中使用数据绑定。 Android studio 本身不会给出任何错误,只有当...

回答 1 投票 0

RecyclerView.Adapter 内的 Android Kotlin ImageView 未按预期更新

最终在我的服务器中实现设备类型的图标后,我用服务器中的图标替换了之前使用的临时可绘制对象,并使用线圈加载它们。他们的 ImageView 是

回答 1 投票 0

数据绑定在等待 Android 中填充值时显示未填充的值

我正在使用数据绑定使用三元语法在 TextView 上显示文本。 android:text="@{vm.value.firstValue != null ? vm.value.firstValue.toString() : @string/source1}" 该代码显示...

回答 1 投票 0

E/iardini.error0:无效 ID 0x00000001 - 错误资源 ID

我目前遇到一个错误,该错误继续降低我的应用程序的性能。 该错误不会导致应用程序关闭或崩溃,也不会导致向控制台写入异常。 ...

回答 2 投票 0

如果我为项目启用数据绑定,我还需要使用 kapt 插件(我正在使用 ksp)吗?

我知道 dataBinding 需要一个注释处理器(由 kapt 提供),是否有办法使用 ksp 来实现此目的?

回答 1 投票 0

错误:包android.databinding不存在

我正在构建我的第一个 Android 应用程序。我正在按照 Android Authority 的本教程制作计算器应用程序:https://www.androidauthority.com/build-a-calculator-app-721910 我有...

回答 1 投票 0

Android Studio 数据绑定 android:text error = <expr> 预期,得到 '''

android:text="@{String.valueOf(detailedPostVM.postLiveData.likedUsers.size)+'人们喜欢'}" android:text="@{detailedPostVM.alreadyLiked ? 'LIKED' : 'LIKE'}" 我正在尝试使用

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.