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

我经历了许多类似的问题,但没有一个答案似乎解决了我的问题.我实现了一个自定义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

这是我的实施:

CustomEditText

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,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的客户端

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools">

name="data"

type="SampleData" />

name="presenter"

type="SamplePresenter" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:animateLayoutChanges="true"

tools:context=".sample_view.presentation.view.SampleView">

android:id="@+id/notificationPopup"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:clipToPadding="false"

android:elevation="4dp"

app:allowManualExit="true" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:gravity="center"

android:orientation="vertical">

android:id="@+id/textView_mirror"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:fontFamily="sans-serif"

android:text="@{data.content}"

android:textSize="16sp"

android:textStyle="bold"

tools:text="test" />

android:id="@+id/customEditText_sample"

style="@style/RegisterInput"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Type anything"

android:text="@={data.content}" />

android:id="@+id/button_validateInput"

style="@style/Widget.AppCompat.Button.Colored"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_marginEnd="8dp"

android:layout_marginStart="8dp"

android:layout_marginTop="8dp"

android:onClick='@{(v) -> presenter.onValidateDataClick(customEditTextSample.getText())}'

android:text="Validate Input" />

P.S.:如果我将CustomEditText替换为常规的EditText小部件,它可以很好地工作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值