【问题标题】:onTouchevent() problem androidonTouchevent() 问题 android
【发布时间】:2011-06-15 11:40:38
【问题描述】:

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

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; 

        }


    }


}

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

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

【问题讨论】:

  • 根据引用,Activity.onTouchEvent() 仅在没有视图消耗事件时调用。也许让您的视图具有焦点使它“吃掉”所有事件? Activity.onTouchEvent() 会被调用吗? (请在您的答案中添加@bigstones,以便我收到通知)
  • 下次正确格式化您的代码,尽量不要发布您的整个代码并让我们通过它。

标签: android view touch-event


【解决方案1】:

找到了!

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

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

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

还有这一行到 onCreate 方法:

previewLayout.setOnClickListener(previewListener);

它成功了!

【讨论】:

  • 我想你可能会看到添加点击监听器的副作用,这意味着setClickable(true)。与其添加一个无用的监听器,不如直接调用setClickable()
【解决方案2】:

您似乎将onTouch 移到了DrawView 之外。由于您正在尝试处理触摸事件,因此您需要设置 onTouchListener 而不是 onClickListener

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多