错误: 为双向数据绑定设置绑定适配器时期望

问题描述 投票:0回答:2

我正在尝试使用TextInputEditText变量为Float设置双向绑定。

这里是我的DataBindingAdapters类的全部内容。

object DataBindingAdapters  {
    @BindingAdapter("android:text")
    @JvmStatic
    fun setText(view: TextInputEditText, value: Float) {
        if(value != view.text.toString().toFloat()) {
            view.setText(value.toString())
        }
    }


    @InverseBindingAdapter(attribute = "android:text")
    fun getText(view: TextInputEditText): Float  {

        return view.text.toString().toFloat()
    }

}

但是我得到:

error: <identifier> expected
            float callbackArg_0 = mBindingComponent.null.getText(inputFieldSize);

我想念的是什么?

android android-databinding
2个回答
0
投票

您又错过了一个绑定适配器,如InverseBindingAdapter documentation中所述:

@BindingAdapter(value = [ "android:textAttrChanged"], requireAll = false)
fun setTextWatcher( view: TextInputEditText, textAttrChanged: InverseBindingListener?) {
    val newValue =  object: TextWatcher {
        override fun afterTextChanged(s: Editable?) = Unit

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit

        override fun onTextChanged( s:CharSequence,  start:Int,  before:Int,  count:Int) {
                textAttrChanged?.onChange()
        }
    }

    val  oldValue = ListenerUtil.trackListener(view, newValue, R.id.textWatcher)
    if (oldValue != null) {
        view.removeTextChangedListener(oldValue)
    }
    view.addTextChangedListener(newValue)
}

创建@InverseBindingAdapter(attribute = "android:text")时,将创建事件android:textAttrChanged,并且必须为其添加绑定适配器。

调用textAttrChanged?.onChange()将触发您创建的InverseBindingAdapter


0
投票

XML可能缺少一个匹配的id;至少它在那里读null

并且TextInputEditText期望TextInputEditText而不是CharSequence

[使用Float,将永远不会用@BindingAdapter("android:text")对其进行调用。

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