我正在尝试向小部件上的 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/