我收到此错误:
无法找到属性的设置者' android:onClick'在android.widget.Button上使用参数类型android.view.View.OnClickListener。 file:/home/itstym/ApkProjects/Reminder/app/src/main/res/layout/activity_main.xml loc:54:27 - 54:61
<data class="ActivityMainBinding">
<variable
name="login"
type="com.example.itstym.reminder.loginViewModel" />
<variable
name="handler"
type="com.example.itstym.reminder.LoginHandler" />
</data>
<Button
android:onClick="@{(v) -> handler.onLoginButtonClicked(v,login)}"
android:id="@+id/submit_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:text="Sign In"
app:layout_constraintEnd_toEndOf="@+id/user_password"
app:layout_constraintStart_toStartOf="@+id/user_password"
app:layout_constraintTop_toBottomOf="@+id/user_password"/>
MainActivity.java
val handler= LoginHandler(this@MainActivity)
LoginHandler.kt
class LoginHandler(context:Context) {
fun onLoginButtonClicked(view:View, userInfo: loginViewModel) {
Log.i("Email is ", userInfo.userEmailAddress)
Log.i("Password is ", userInfo.userPassword)
}
}
at android.databinding.tool.processing.Scope.assertNoError(Scope.java:112)
at android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:91)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$200(JavacProcessingEnvironment.java:91)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.runContributingProcs(JavacProcessingEnvironment.java:627)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1033)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1198)
at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1068)
at org.jetbrains.kotlin.kapt3.AnnotationProcessingKt.doAnnotationProcessing(annotationProcessing.kt:73)
如何在此处为按钮创建setter?我想我只是为视图创建它,而不是为按钮设置器创建它。
{{1}}
任何线索都会有所帮助!
答案 0 :(得分:-1)
您的xml
应该是这样的
<data class="ActivityMainBinding">
<variable
name="login"
type="com.example.itstym.reminder.loginViewModel" />
<variable
name="handler"
type="com.example.itstym.reminder.MainActivity" />
</data>
<Button
android:onClick="@{(v) -> handler.onLoginButtonClicked(v, login)}"
android:id="@+id/submit_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:text="Sign In"
app:layout_constraintEnd_toEndOf="@+id/user_password"
app:layout_constraintStart_toStartOf="@+id/user_password"
app:layout_constraintTop_toBottomOf="@+id/user_password"/>
您可以直接在onClick
中获取此MainActivity
。无需创建单独的类。
<强> MainActivity.java 强>
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setHandler(this);
然后在MainActivity
中,您可以访问此方法,如:
public void onLoginButtonClicked(View view, LoginViewModel model) {
//Do something
}