2012-04-14 20 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

相關問題