gpt4 book ai didi

android - onTouchevent() 问题android

转载 作者:行者123 更新时间:2023-11-29 00:48:41 27 4
gpt4 key购买 nike

当我运行下面的代码时,一切正常,这是一个简单的应用程序,带有三个可以移动的球...

public class dragndrop extends Activity {
/** Called when the activity is first created. */
private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls
private static final String TAG="MyTAG";
DrawView myView;
private int balID = 0; // variable to know what ball is being dragged
int X;
int Y;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Point point1 = new Point();
point1.x = 50;
point1.y = 20;
Point point2 = new Point();
point2.x = 100;
point2.y = 20;
Point point3 = new Point();
point3.x = 150;
point3.y = 20;


// declare each ball with the ColorBall class
colorballs[0] = new ColorBall(this,R.drawable.bol_groen, point1);
colorballs[1] = new ColorBall(this,R.drawable.bol_rood, point2);
colorballs[2] = new ColorBall(this,R.drawable.bol_blauw, point3);
myView = new DrawView(this);
setContentView(myView);
}


public class DrawView extends View {


public DrawView(Context context) {
super(context);
setFocusable(true); //necessary for getting the touch events

// setting the start point for the balls


}

// the method that draws the balls
@Override protected void onDraw(Canvas canvas) {

//draw the balls on the canvas
for (ColorBall ball : colorballs) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
}

}



// events when touching the screen
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();

X = (int)event.getX();
Y = (int)event.getY();

switch (eventaction ) {

case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
balID = 0;
for (ColorBall ball : colorballs) {
Log.d(TAG,"inside action down inside for coords:"+X+" coords: "+Y);
Log.d(TAG,"ball coords:"+ball.getX()+" coords: "+ball.getY());

int x =X;
int y =Y;
Log.d(TAG,"lalalalalala"+x+" coords: "+y);



if (x > ball.getX() && x < ball.getX()+50 && y > ball.getY() && y < ball.getY()+50){//if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
Log.d(TAG,"inside ball coords!!!!!!!!!!!!!!!!!!!!!!!!:"+ball.getX()+" coords: "+ball.getY());
balID = ball.getID();


break;
}
}

break;


case MotionEvent.ACTION_MOVE: // touch drag with the ball
// move the balls the same as the finger
if (balID > 0) {
colorballs[balID-1].setX(X-25);
colorballs[balID-1].setY(Y-25);
}

break;

case MotionEvent.ACTION_UP:
// touch drop - just do things here after dropping

break;
}
// redraw the canvas
myView.invalidate();
return true;

}


}


}

但是当我尝试处理主要 Activity 中的 onTouchevent 时不起作用,奇怪的是它无法读取简单变量(x,y)!!!我不明白为什么会这样,似乎只有在 View 中才能使它们变红!!!

public class dragndrop extends Activity {
/** Called when the activity is first created. */
private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls
private static final String TAG="MyTAG";
DrawView myView;
private int balID = 0; // variable to know what ball is being dragged
int X;
int Y;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Point point1 = new Point();
point1.x = 50;
point1.y = 20;
Point point2 = new Point();
point2.x = 100;
point2.y = 20;
Point point3 = new Point();
point3.x = 150;
point3.y = 20;


// declare each ball with the ColorBall class
colorballs[0] = new ColorBall(this,R.drawable.bol_groen, point1);
colorballs[1] = new ColorBall(this,R.drawable.bol_rood, point2);
colorballs[2] = new ColorBall(this,R.drawable.bol_blauw, point3);
myView = new DrawView(this);
setContentView(myView);
}


// events when touching the screen
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();

X = (int)event.getX();
Y = (int)event.getY();

switch (eventaction ) {

case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
balID = 0;
for (ColorBall ball : colorballs) {
Log.d(TAG,"inside action down inside for coords:"+X+" coords: "+Y);
Log.d(TAG,"ball coords:"+ball.getX()+" coords: "+ball.getY());

int x =X;
int y =Y;
Log.d(TAG,"lalalalalala"+x+" coords: "+y);



if (x > ball.getX() && x < ball.getX()+50 && y > ball.getY() && y < ball.getY()+50){//if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
Log.d(TAG,"inside ball coords!!:"+ball.getX()+" coords: "+ball.getY());
balID = ball.getID();


break;
}
}

break;


case MotionEvent.ACTION_MOVE: // touch drag with the ball
// move the balls the same as the finger
if (balID > 0) {
colorballs[balID-1].setX(X-25);
colorballs[balID-1].setY(Y-25);
}

break;

case MotionEvent.ACTION_UP:
// touch drop - just do things here after dropping

break;
}
// redraw the canvas
myView.invalidate();
return true;

}




public class DrawView extends View {


public DrawView(Context context) {
super(context);
setFocusable(true); //necessary for getting the touch events

// setting the start point for the balls


}

// the method that draws the balls
@Override protected void onDraw(Canvas canvas) {
//canvas.drawColor(0xFFCCCCCC); //if you want another background color

//draw the balls on the canvas
for (ColorBall ball : colorballs) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
}

}


}


}

谁知道为什么?

是的@bigstones onTouchevent 正在工作,它捕获所有操作,问题是如果我在 Activity 中有 ontouchevent 代码,变量 X、Y 似乎不起作用,尽管它们有一个值并且我可以打印它(或日志)我说的是经过测试的,我已经尝试将 if() 语句(getX,getY)中的所有值更改为整数,但它不仅适用于 X,Y .....检查请再次输入代码!谢谢!

最佳答案

找到了!

我发现问题在于应该注册某种监听器的布局,所以我这样做了:

我将此代码添加到我的主类中:

private OnClickListener previewListener = new OnClickListener() {       
@Override
public void onClick(View v) {
System.err.println("I've been clicked");
}
};

onCreate 方法的这一行:

previewLayout.setOnClickListener(previewListener);

成功了!

关于android - onTouchevent() 问题android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4798751/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com