尝试在触摸屏幕时开始新的活动

问题描述:

我试图在屏幕被触摸时开始新的活动。背景是调整后的位图。但没有检测到触摸。这里是我的代码:尝试在触摸屏幕时开始新的活动

公共类DispCover扩展活动实现OnTouchListener {

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    bg = new DispCoverG(this); 
    setContentView(bg); 
    } 



@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 


} 
@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    bg.pause(); 
} 
@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    bg.resume(); 
} 

float dtp(float dp,Context context){ 
    Resources resources = context.getResources(); 
    DisplayMetrics metrics = resources.getDisplayMetrics(); 
    float px = dp * (metrics.densityDpi/160f); 
    return px; 
} 

@Override 
public boolean onTouch(View v, MotionEvent e) { 
    int action = e.getAction(); 
    int xd = (int) e.getX(); 
    int yd = (int) e.getY(); 

    int x = (int) dtp(xd,this); 
    int y = (int) dtp(yd,this); 

    switch(action){ 
    case MotionEvent.ACTION_DOWN: 
    startActivity(new Intent(//)); 
    break; 
    } 
    return true; 
} 

public class DispCoverG extends SurfaceView implements Runnable{ 



    public DispCoverG(Context context) { 
     //code 
    } 

    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) 
    { 
     //code 
    } 

    public void pause(){ 
     //code 
    } 

    public void resume(){ 
     //code 
    } 


    @Override 
    public void run() { 
     //code 

     } 
    } 
} 

};

在此先感谢。

setContentView(bg);

bg.setOnTouchListener(this); 

OR

bg.setOnTouchListener(new OnTouchListener() { 
    public boolean onTouch(View v, MotionEvent event) { 
     // TODO Auto-generated method stub 
     int action = event.getAction(); 
     switch(action){ 
      case MotionEvent.ACTION_DOWN: 
      startActivity(new Intent(YourActivity.this,NewActivity.class)); 
      break; 
     } 
     return true; 
    } 
}); 

下下面的代码实现活动OnTouchListener

+0

[Rü得到你想要的东西? – AkashG 2012-07-19 11:49:19

+0

发布问题后,我想那是失踪。无论如何,非常感谢。 – Crashh 2012-07-23 07:47:19

另一种方法是直接赶MotionEvents在您的视图:

public class DispCoverG extends SurfaceView implements Runnable{ 
.... 
    @Override 
    public boolean onTouchEvent(MotionEvent event){ 
     // to dispatch click/long click event, 
     // you must pass the event to it's default callback View.onTouchEvent 
     boolean defaultResult = super.onTouchEvent(event); 

     ... 
     switch (event.getAction()){ 
      case MotionEvent.ACTION_DOWN: 
       Intent intent = new Intent(DispCover.this, XActivity.class); 
       startActivity(intent); 
       break; 
      } 
     return defaultResult; 
     //return true; //this should work if you need just ACTION_DOWN 
    } 
} 
+0

非常感谢。 – Crashh 2012-07-23 07:49:46