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

时间:2019-08-25 23:06:34

标签: java android events button widget

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

我的小部件xml是:

var arr=[[{name:'kiwi',value:12},{name:'apple',value:5},{name:'apple',value:12},{name:'pizza',value:33},{name:'pizza',value:24},{name:'fish',value:5},{name:'milk',value:5},{name:'banana',value:7},{name:'orange',value:11}],[{name:'taco',value:23},{name:'pizza',value:78},{name:'apple',value:12},{name:'pizza',value:33},{name:'pizza',value:24},{name:'fish',value:5},{name:'pie',value:1},{name:'cake',value:3},{name:'banana',value:7},{name:'beef',value:123},{name:'lime',value:72},{name:'pizza',value:34}],[{name:'apple',value:12},{name:'pizza',value:33},{name:'pizza',value:24},{name:'pizza',value:23},{name:'fish',value:5},{name:'banana',value:7},{name:'banana',value:77}]];


/* Flatten array heirachy */
const flatArr = arr.flat();

/* Obtain a count mapping for each object's occourance in flatArr */
const mapObjectToCount = flatArr.reduce((map, item) => {

  const key = JSON.stringify(item);
  const count = (map[key] ? map[key] : 0) + 1;
  
  return { ...map, [ key ] : count };
}, {})

/* Get key/value pair of the prior mapping, filter the objects by
those that occour more that one time, and obtain the original object
by parsing the key */
const result = Object.entries(mapObjectToCount)
.filter(([json, count]) => count > 1)
.map(([json]) => JSON.parse(json));

console.log(result)

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

<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>

所以

我该怎么办?

非常感谢。

致谢。

1 个答案:

答案 0 :(得分:0)

在布局中添加

  

android:clickable =“ true”

在ImageButton标记中。

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