2012-04-14 74 views
2

这里是我的代码 - PadsGrid是一个ViewGroup - :为什么我的ViewGroup的孩子触摸事件不起作用?

public class Emc_PadControllerActivity extends Activity implements OnTouchListener { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     final PadsGrid pg = new PadsGrid(this, 8, 5, PadType.SMALL); 
     for (int i=0;i<pg.getChildCount();i++){ 
      final PadController pc; 
      pc=(View) pg.getChildAt(i); 
      pc.setOnTouchListener(new View.OnTouchListener() { 
       @Override 
       public boolean onTouch(View arg0, MotionEvent arg1) { 
        pc.onTouch(arg0,arg1); 
        return true; 
       }});; 
     } 

     setContentView(pg); 

    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     v.onTouchEvent(event); 
     return false; 
    } 
} 

在这方面,onTouch事件我的观点is'nt打电话,为什么我摸不到,为什么?

回答

1

你想你的onTouchlistener设置为Emc_PadControllerActivity.this而不是使用匿名内部类这样:

pc.setOnTouchListener(Emc_PadControllerActivity.this) 

这将调用您的onTouch()中的主类。在这里,您可以确定哪个视图被点击并采取相应的行动。

看看我前面question here.

+0

好吧,但我想直接导管的的onTouchEvent的意见。 因为这种方式更快,更容易处理多点触控事件! – nonozor 2012-04-14 17:02:04

+0

在这种情况下,你应该可以'pc.setOnTouchListener(pc.onTouch());'...我假设pc类有一个onTouch()方法。 – cstrutton 2012-04-14 17:06:02

+0

当然可以,但是如何传递两个参数:Event和View到onTocuh方法? – nonozor 2012-04-14 17:12:05