java - 通过小部件中的按钮触发事件

标签 java android events button widget

我正在尝试向小部件上的 ImageButton 添加监听器,但我不能。

我的小部件 xml 是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#09C"
    android:padding="@dimen/widget_margin"
    >


    <ImageButton
        android:id="@+id/activityCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="#09C"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"       
        android:text="Hecho"/>

</RelativeLayout>

按下 imageButton“activityCheck”时我需要执行的方法是下一个:


    public void changeColor(View view) {
          Log.d("changeColor", "PASE POR AQUÍ");
       ImageButton button = view.findViewById(R.id.activityCheck);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                _changeColor(v);
            }
        });
    }


我该怎么做?

最佳答案

在布局中添加

android:clickable="true"

在 ImageButton 标签中。

这将使您的图像可点击,然后它将检测任何点击事件。

关于java - 通过小部件中的按钮触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57650323/

相关文章:

java - 使用十六进制值设置 Android 图像

jQuery 和清除事件监听器

java - Android Lunar Lander 线程处理替代方案

java - 嵌套数据结构问题

java - 从并行数组中搜索和显示信息

android - 如何在 Facebook Android sdk 4.0 中使用图形请求获取登录用户的电子邮件地址

android - 缩放 Google map 并在 Android 上创建折线

javascript - 单击按钮后尝试删除输入并了解为什么在控制台中数组未更新

JavaScript 闭包和变量作用域

Java : "GC Overhead limit exceeded" despite plenty of memory to allocate available