How to open a new Intent inside OnTouch Event?

人盡茶涼 提交于 2019-12-30 07:47:46

问题


I have a customized OnTouch Method where i check if the touch coordinates contains a rectangle.After checking the condition i want to start a new Activity.But unfortuanlely its not workin.Can someone help me out?

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;
}
}

回答1:


Fist of all, when you log, you don't use System.out.println(");. For logging in Android one uses the Log class, so you can simply log like this: Log.d("className", "This is a test");

This might resolve your problem The method startActivity(Intent) is undefined for the type?

I hope this helps.




回答2:


I found the answer :)

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);
}


来源:https://stackoverflow.com/questions/12032744/how-to-open-a-new-intent-inside-ontouch-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!