Android can';t按一下按钮绑定
我的应用程序中遇到了一个奇怪的问题。我无法将按钮的onClick事件绑定到方法。这是我的密码:Android can';t按一下按钮绑定,android,button,data-binding,onclick,Android,Button,Data Binding,Onclick,我的应用程序中遇到了一个奇怪的问题。我无法将按钮的onClick事件绑定到方法。这是我的密码: public class BookingViewModel extends BaseObservable { public void onAddInvite(View view) { invitationList.add(new Person(name, number, email)); notifyPropertyChanged(BR.invitations
public class BookingViewModel extends BaseObservable {
public void onAddInvite(View view) {
invitationList.add(new Person(name, number, email));
notifyPropertyChanged(BR.invitationsText);
}
}
xml:
xml:
所有其他绑定都工作得非常好。另外,androidstudio使该方法看起来像正在使用。我做错了什么?尝试在按钮中设置android:clickable=“true”,就像您在工作案例中所做的那样。您可以使用click侦听器来捕获click事件。此外,您还可以使用(单击以获取更多信息)进行字段和方法绑定(这就是我现在使用的) 尝试在按钮中设置android:clickable=“true”,就像您在工作案例中所做的那样。您可以使用click侦听器来捕获click事件。此外,您还可以使用(单击以获取更多信息)进行字段和方法绑定(这就是我现在使用的) 我很高兴这有帮助!我很高兴这有帮助!
<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>
<variable
name="bookingViewModel"
type="it.jgrassi.roombooking.viewmodel.BookingViewModel" />
</data>
<Button
android:id="@+id/button_book"
android:layout_width="89dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Book"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:enabled="@{bookingViewModel.bookingEnabled}"
android:onClick="@{bookingViewModel::onAddInvite}"/>
</layout>
public class ItemRoomViewModel extends BaseObservable{
public void onItemClick(View view) {
//do stuff
}
}
<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>
<variable
name="roomViewModel"
type="it.jgrassi.roombooking.viewmodel.ItemRoomViewModel" />
</data>
<android.support.constraint.ConstraintLayout
android:id="@+id/item_room"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:onClick="@{roomViewModel::onItemClick}"
android:paddingBottom="8dp"
android:paddingTop="8dp">
</layout>