【问题标题】:How to open a new Intent inside OnTouch Event?如何在 OnTouch 事件中打开一个新的 Intent?
【发布时间】:2012-08-20 05:42:41
【问题描述】:

我有一个自定义的 OnTouch 方法,我检查触摸坐标是否包含一个矩形。检查条件后我想开始一个新的 Activity。但不幸的是它不起作用。有人可以帮我吗?

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

【问题讨论】:

    标签: android-intent android-activity drawrect touch-event


    【解决方案1】:

    首先,当您登录时,您不使用System.out.println(");。登录 Android 时使用 Log 类,因此您可以像这样简单地登录:Log.d("className", "This is a test");

    这可能会解决您的问题The method startActivity(Intent) is undefined for the type?

    我希望这会有所帮助。

    【讨论】:

      【解决方案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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-03
        • 2012-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-28
        相关资源
        最近更新 更多