2012-03-03 24 views
1

我動態添加了自定義的View,但我不知道如何爲它們設置點擊偵聽器。動態設置OnClickListener以自定義視圖

我的代碼:

rect1.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v){ 
      frame_view.addView(new CustomView(getApplicationContext()) 
      .setOnLongClickListener(listener); 
     } 
    }); 

Rect1的=我的按鈕。

Eclipse中強調addView,並顯示此錯誤:

The method addView(View) in the type ViewGroup is not applicable for the arguments (void)

回答

1
CustomView objCustomView=new CustomView(getApplicationContext()); 
objCustomView.setOnLongClickListener(listener); 
frame_view.addView(objCustomView) 
+0

使用'ViewGroup'允許設置單擊方法'addView' lisnener? – budgie 2012-03-03 11:56:07

+0

看到答案我更新....抱歉,我不能理解你的問題第一次... – 2012-03-03 12:02:37

相關問題