android - 数据绑定(bind)按钮onclick不起作用

标签 android android-databinding

activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

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

        <variable
            name="callback"
            type="com.buscom.ActionCallBack" />
    </data>

    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/ll_oml"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/grey_50"
        android:orientation="vertical">

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="@{(v) -> callback.onClick(v)}"
                android:text="Menu" />
        </android.support.design.widget.CoordinatorLayout>
    </LinearLayout>
</layout>

ActionCallBack.java

这是我在MainActivity中实现的接口(interface)

public interface ActionCallback { 
    void onClick(View view);
}

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    actionCallBack = new ActionCallBack() {

        @Override
        public void onClick(View view) {
            System.out.println("Call onclick method *****");
        }
    }
}

当我单击按钮时,未调用 onClick() 方法,输出中显示注释或未执行任何操作。但是它以传统方式与 onClickListener 一起工作

最佳答案

我认为您的 Activity 声明有误。无论如何,你仍然没有设置你的回调,这样:

binding.setCallback(this);

binding.setCallback(actionCallback);

关于android - 数据绑定(bind)按钮onclick不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41938671/

相关文章:

java - RuntimeException : view tag isn't correct on view:null on ViewAgendaItemBinding. 绑定(bind)()方法

android - 如何将数据从 recyclerview 中单击的项目传递到 viewmodel 并打开新 Activity ?

android - 在 onClick 事件中获取 View 的 id 作为 String

Android Studio - flavor 可以与其他 flavor 共享代码吗?

android - Gradle在哪里找到 'proguard-android-optimize.txt'?

android - 在Android Studio中导入Maven项目

android - 使用二进制文字数字时出错。为什么要使用旧的JDK?

android - 寻找回调以了解数据绑定(bind)何时完成

android - 如何为android数据绑定(bind)点击监听器编写proguard规则?

android - Activity 不显示 RecyclerView - 数据绑定(bind)