Java Android的触摸次数翻倍?

Java Android的触摸次数翻倍?,java,android,touch,Java,Android,Touch,这个android应用程序就像我每次触摸屏幕时都双击它一样,有人能看到为什么会发生这种情况吗?我让它记录“触摸”+[触摸次数],它同时执行“触摸1”和“触摸2”,与“触摸3”和“触摸4”相同 package com.example.mondrianmaker; 导入java.util.ArrayList; 导入java.util.Random; 导入android.app.Activity; 导入android.graphics.Bitmap; 导入android.graphics.Canvas

这个android应用程序就像我每次触摸屏幕时都双击它一样,有人能看到为什么会发生这种情况吗?我让它记录“触摸”+[触摸次数],它同时执行“触摸1”和“触摸2”,与“触摸3”和“触摸4”相同

package com.example.mondrianmaker;
导入java.util.ArrayList;
导入java.util.Random;
导入android.app.Activity;
导入android.graphics.Bitmap;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.graphics.Point;
导入android.graphics.drawable.BitmapDrawable;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Display;
导入android.view.MotionEvent;
导入android.view.Window;
导入android.view.WindowManager;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
int宽度、高度、x、y、颜色、触感;
ArrayList矩形,childsRect;
位图背景;
帆布;
显示;
线性布局;
点大小;
油漆;
随机rn;
矩形parentRect;
文本查看文本,信息;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//***全屏***
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,WindowManager.LayoutParams.FLAG_全屏);
//---全屏---
setContentView(R.layout.activity_main);
display=getWindowManager().getDefaultDisplay();
大小=新点();
display.getSize(size);
宽度=尺寸x;
高度=尺寸y;
paint=new paint();//绘制到颜色矩形斜
bg=Bitmap.createBitmap(480800,Bitmap.Config.ARGB_8888);
画布=新画布(bg);
ll=(线性布局)findViewById(R.id.mondrian);
矩形=新的ArrayList();
text=(TextView)findViewById(R.id.coordinates);
info=(TextView)findViewById(R.id.info);
parentRect=新矩形(0,0,480,800);//创建超级父矩形
矩形。添加(parentRect);//将超级父矩形添加到列表中
触摸=0;
}
@凌驾
公共布尔onTouchEvent(运动事件){
//获得坐标
x=(int)event.getX();
y=(int)event.getY();
//将文本视图设置为单击的坐标和列表上的矩形数
text.setText(x+,“+y+”矩形:“+rectangles.size());
//随机设置矩形颜色*
setColor();
油漆。设置颜色(颜色);
//单击“获取矩形”
parentRect=getRectParent(x,y);
//单击矩形的子对象
childsRect=makeChilds(parentRect,x,y);
addChildsToRectanglesDeleteParentRect(childsRect,parentRect);
//检查孩子们
//text.setText(childsRect.get(0)+“”+childsRect.get(1));
//采取行动
开关(event.getAction()){
//case MotionEvent.ACTION\u DOWN:
//text.setText(x+“,”+y);
//ll.setBackgroundDrawable(新位图绘制(bg));
//case MotionEvent.ACTION\u移动:
//ll.setBackgroundDrawable(新位图绘制(bg));
//System.out.println(“22222”);
case MotionEvent.ACTION\u UP:
触摸++;
Log.i(“信息”、“触摸”+触摸);
//ll.setBackgroundDrawable(新位图绘制(bg));
//系统输出打印号(“33333”);
}
返回false;
}
public void addChildsToRectanglesDeleteParentRect(ArrayList childsRect,矩形parentRect){
矩形。删除(parentRect);
add(childsRect.get(0));
add(childsRect.get(1));
int i=0;
Log.i(“info”,“length=“+rectangles.size());
用于(矩形e:矩形){
Log.i(“info”,i+“-->”+e.toString());
i++;
}
}
公共ArrayList makeChilds(矩形父对象,整数x,整数y){
ArrayList默认值=新建ArrayList();
添加(新矩形(0,0240800));
添加(新矩形(240,0480800));
ArrayList childs=新的ArrayList();
用于(矩形:矩形){
如果(x>=rect.getX1()&&x=rect.getY1()&&y0){
返回儿童;
}否则{
info.setText(x+,“+y+”+“+parent+”“此父级无子级”);
返回默认值;
}
}
公共矩形getRectParent(整数x,整数y){
用于(矩形g:矩形){
如果(x>g.getX1()&&xg.getY1()&&y
你可能会得到一个
动作
和一个
动作
并同时计数


尝试将您的
++
移动到开关盒内,以便执行
操作_DOWN

我测试了您的代码,可以正常工作,但是您可以尝试使用if语句。另外,请确保您的switch语句返回true或break;虽然它可以正常工作,但不使用任何一个;下面是如何使用if语句:

 if(event.getAction() == MotionEvent.ACTION_UP){
           // up is called
           touch++;
           Log.i("info", "touch # " + touch);
           return true;
       }

事实上,我把它放在了ACTION_UP案例中,并且仍然计算了两次。你的案例都没有
bre
 if(event.getAction() == MotionEvent.ACTION_UP){
           // up is called
           touch++;
           Log.i("info", "touch # " + touch);
           return true;
       }