如何打开OnTouch事件中一个新的意图是什么?(How to open a new Intent

2019-06-27 04:07发布

我有一个自定义OnTouch方法,其中我检查触摸坐标包含rectangle.After检查我要开始一个新的Activity.But unfortuanlely它不是workin.Can有人能帮我出了状况?

public class custom extends SurfaceView implements SurfaceHolder.Callback{


public boolean onTouchEvent(MotionEvent event) {
    int touchX = (int) event.getX();
    int touchY = (int) event.getY();
    switch(event.getAction()){

    case MotionEvent.ACTION_DOWN:
            System.out.println("Touching down!");

            for(int i =0; i< rectangles.size();i++){

                if(rectangles.get(i).contains(touchX,touchY)){
                    System.out.println("Touched Rectangle, start activity.");
                    rectangles.get(i).describeContents ();
                    Selected_rect = String.valueOf(rectangles.get(i));
                                       }
                Intent intent=new Intent(getContext(), DetectBlock.class);  
                startActivity(intent); //ERROR >> Start activity undefined   

            }
            break;
    case MotionEvent.ACTION_UP:
            System.out.println("Touching up!");
            break;
    case MotionEvent.ACTION_MOVE:
            System.out.println("Sliding your finger around on the screen.");
            break;
    }
    return true;
}
}

Answer 1:

所有的拳,当你登录,您不使用System.out.println(");对于记录在Android的人使用的Log类,所以你可以简单地记录这样的: Log.d("className", "This is a test");

这可能会解决您的问题startActivity(意向)是未定义的类型的方法?

我希望这有帮助。



Answer 2:

我找到了答案:)

private void startActivity() {
    // TODO Auto-generated method stub
    Intent myIntent = new Intent("android.intent.action.DetectBlock");
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getContext().startActivity(myIntent);
}


文章来源: How to open a new Intent inside OnTouch Event?