简体   繁体   English

onTouchEvent用于查看多个图像

[英]onTouchEvent for multiple images in view

In my Java class we are doing some Android code. 在我的Java课中,我们正在做一些Android代码。 We create a View and pragmatically generate 10 ImageButton (custom button image class with no extensions). 我们创建一个View并实用地生成10个ImageButton(不带扩展名的自定义按钮图像类)。 The teacher suggests that we loop through an array containing our 10 ImageButtons to see which ImageButton contains the point of touch (in our onTouchEvent method). 老师建议我们遍历包含10个ImageButton的数组,以查看哪个ImageButton包含触摸点(在我们的onTouchEvent方法中)。 Is there a better way to do this, like for example create an onTouchEvent for the actually ImageButton class? 有没有更好的方法来执行此操作,例如为实际的ImageButton类创建onTouchEvent?

Why so complicated? 为什么这么复杂? Just use a OnClickListener for all the buttons and inside the onClick method just switch by the id of the clicked view like this: 只需对所有按钮使用OnClickListener ,并且在onClick方法内部,只需按如下所示的点击视图的ID进行切换即可:

switch(view.getId()){
    case button1.getId():
    break;
    case button2.getId():
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM