2013-11-28 111 views
4

我有一個FrameLayout表,其中每個幀包含ImageViewTextView。無論幀中的內容如何,​​我都希望onClick事件能被幀上設置的OnClickListener檢測到。如何讓點擊事件傳遞給Android中的容器?

我怎樣才能做到這一點?

這是我的一些佈局,我總共有5行(這裏只顯示1)。 如上所述,每行有5個FrameLayouts,每個包含一個TextView。我無法將我的OnClickListener放在TextView上,因爲這可能會在運行時更改爲ImageView。因此我想要FrameLayout上的OnClickListener。

看來,FrameLayout的內容阻止它檢測到點擊事件。

<TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/gateContainer" 
     android:stretchColumns="*"> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center"> 

      <FrameLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/door1" 
       android:clickable="true"> 

       <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:text="New Text" 
        android:id="@+id/textView" 
        android:layout_gravity="center" /> 

      </FrameLayout> 

      <FrameLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/door2" > 

       <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:text="New Text" 
        android:id="@+id/textView5" 
        android:layout_gravity="center" /> 
      </FrameLayout> 

      <FrameLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/door3" > 

       <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:text="New Text" 
        android:id="@+id/textView4" 
        android:layout_gravity="center"/> 
      </FrameLayout> 
    </TableLayout> 

下面是我如何設置OnClickListeners一個例子:

 View.OnClickListener clickListener = new View.OnClickListener() { 
      @Override 
      public void onClick(View v){ 
       // do something 
      } 
     }; 

     TableLayout tableLayout = (TableLayout) findViewById(R.id.gateContainer); 
     // Go through all TableRows 
     for (int i = 0; i < tableLayout.getChildCount(); i++){ 
      TableRow tableRow = (TableRow) tableLayout.getChildAt(i); 
      // Set listener for each FrameView 
      for (int j = 0; j < tableRow.getChildCount(); j++){ 
       FrameLayout frame = (FrameLayout) tableRow.getChildAt(j); 

       frame.setOnClickListener(clickListener); 
      } 
     } 
+1

哪裏是你的代碼? – GrIsHu

+2

發佈一些代碼給我們看看你迄今爲止所嘗試過的。 –

+0

@GrIsHu代碼已添加。 – frmi

回答

7

只實現,OnTouchListener通過ImageView和TextView組件,並在其中傳遞它們。

<your_view>.setOnTouchListener(new OnTouchListener(){ 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     // TODO Auto-generated method stub 
     return true; 
    } 

}); 

這將確保您的容器處理該問題。

+0

謝謝,這就是我一直在尋找的東西。我將我的OnClickListener更改爲OnTouchListener。 – frmi

0

您可以在TextViewImageView如下定義click事件:

<yourView>.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // your logic here. 

     } 
    }); 
相關問題