如何解决:使用自定义视图实现双向数据绑定时“找不到属性'android:text'的getter”?

Posted

技术标签:

【中文标题】如何解决:使用自定义视图实现双向数据绑定时“找不到属性\'android:text\'的getter”?【英文标题】:How to solve: "cannot find getter for attribute 'android:text'" when implementing two-way data binding with custom view?如何解决:使用自定义视图实现双向数据绑定时“找不到属性'android:text'的getter”? 【发布时间】:2018-05-30 16:40:29 【问题描述】:

我遇到了许多类似的问题,但似乎没有一个答案能解决我的问题。我实现了一个自定义EditText,我希望它与双向数据绑定兼容。问题是,每次我尝试编译时都会出现错误:

Error:java.lang.IllegalStateException: failed to analyze: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Cannot find the getter for attribute 'android:text' with value type java.lang.String on com.app.toolkit.presentation.view.CustomEditText. file:/Users/humble-student/Home/workspace/android/application/app/src/main/res/layout/login_view.xml loc:68:8 - 81:69 ****\ data binding error ****

    at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:137)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:158)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:61)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:107)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:51)
    at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:92)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:386)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:96)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:892)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:96)
    at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:919)
    at 

这是我的实现:

自定义编辑文本

class CustomEditText @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) 

    // ...
    private lateinit var editText_input: EditText
    private lateinit var textView_errorMessage: TextView

    private var isErrorDisplayed = false
    private var inputTextOriginalColor: ColorStateList? = null

    init 
        orientation = VERTICAL
        clearContainerFormatting()

        createEditTextInput(context, attrs, defStyleAttr)
        createTextViewErrorMessage(context)

        addView(editText_input)
        addView(textView_errorMessage)
    

    fun setError(message: String) 
        //...
    

    fun getText(): String = editText_input.text.toString()

    fun setText(text: String) = editText_input.setText(text)

    // ...

型号

data class SampleData(
        private var _content: String
) : BaseObservable() 
    var content: String
        @Bindable get() = _content
        set(value) 
            _content = value
            notifyPropertyChanged(BR.content)
        

使用带有数据绑定的 CustomView 的客户端

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <import type="android.view.View" />

        <variable
            name="data"
            type="SampleData" />

        <variable
            name="presenter"
            type="SamplePresenter" />
    </data>

    <RelativeLayout
        android:layout_
        android:layout_
        android:animateLayoutChanges="true"
        tools:context=".sample_view.presentation.view.SampleView">

        <NotificationPopup
            android:id="@+id/notificationPopup"
            android:layout_
            android:layout_
            android:clipToPadding="false"
            android:elevation="4dp"
            app:allowManualExit="true" />

        <LinearLayout
            android:layout_
            android:layout_
            android:layout_centerInParent="true"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView_mirror"
                android:layout_
                android:layout_
                android:fontFamily="sans-serif"
                android:text="@data.content"
                android:textSize="16sp"
                android:textStyle="bold"
                tools:text="test" />

            <CustomEditText
                android:id="@+id/customEditText_sample"
                style="@style/RegisterInput"
                android:layout_
                android:layout_
                android:hint="Type anything"
                android:text="@=data.content" />

            <Button
                android:id="@+id/button_validateInput"
                style="@style/Widget.AppCompat.Button.Colored"
                android:layout_
                android:layout_
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:onClick='@(v) -> presenter.onValidateDataClick(customEditTextSample.getText())'
                android:text="Validate Input" />
        </LinearLayout>
    </RelativeLayout>
</layout>

P.S.:如果我将 CustomEditText 替换为常规的 EditText 小部件,它可以完美运行

【问题讨论】:

【参考方案1】:

很有趣,但我找到了一个很棒的 post on medium 来帮助我解决这个问题。基本上我需要的是CustomEditTextBinder:

@InverseBindingMethods(
        InverseBindingMethod(
                type = CustomEditText::class,
                attribute = "android:text",
                method = "getText"
        )
)
class CustomEditTextBinder 
    companion object 
        @JvmStatic
        @BindingAdapter(value = ["android:textAttrChanged"])
        fun setListener(editText: CustomEditText, listener: InverseBindingListener?) 
            if (listener != null) 
                editText.addTextChangedListener(object : TextWatcher 
                    override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) 

                    

                    override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) 

                    

                    override fun afterTextChanged(editable: Editable) 
                        listener.onChange()
                    
                )
            
        

        @JvmStatic
        @BindingAdapter("android:text")
        fun setText(editText: CustomEditText, text: String?) 
            text?.let 
                 if (it != editText.text) 
                     editText.text = it
                 
            
         

这可能看起来很奇怪,但您实际上不需要在任何地方调用它,只需添加类,框架将负责通过注释处理找到它。请注意,setText 对于防止无限循环非常重要。我还补充说:

var text: String?
    get() = editText_input.text.toString()
    set(value) 
        editText_input.setText(value)
    

fun addTextChangedListener(listener: TextWatcher) =
        editText_input.addTextChangedListener(listener)

CustomEditText.

这是example of the implementation

【讨论】:

以上是关于如何解决:使用自定义视图实现双向数据绑定时“找不到属性'android:text'的getter”?的主要内容,如果未能解决你的问题,请参考以下文章

vue组件双向绑定key的作用

彻底理解vue双向数据绑定

解决vuejs 创建数据后设置对象的属性实现不了双向绑定问题

解决vuejs 创建数据后设置对象的属性实现不了双向绑定问题

vue3自定义组件使用v-model实现双向数据绑定

Angular自定义组件实现数据双向数据绑定