6

我有一個ImageButton的,幷包裹在的LinearLayout這樣一個TextView:捕獲的LinearLayout的onclick事件

<LinearLayout android:orientation="vertical" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_weight="20" android:gravity="center" 
     android:clickable="true" android:id="@+id/action_showhide"> 
     <ImageButton android:id="@+id/test" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:src="@drawable/ic_toggle_hide_states" android:background="@null"></ImageButton> 
     <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="@string/txtHide" 
      android:textColor="@drawable/orange" android:textStyle="bold"></TextView> 
    </LinearLayout> 

的ImageButton的是通過自定義繪製支持正常,重點突出,壓入狀態。我想讓用戶點擊LinearLayout中的任何地方來觸發一個OnClick事件。下面的代碼顯示設置爲OnClickListener:當用戶點擊任意位置上的TextView但是當用戶點擊ImageButton的事件不能上升到的LinearLayout

final LinearLayout actionHide = (LinearLayout) findViewById(R.id.action_showhide); 
    actionHide.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.d(AppAdvocate.TAG, "Event caught"); 
     } 
    }); 

代碼工作。我沒有爲該按鈕定義onClickListener。我想讓我的ImageButton的drawable改變,所以我不想將它設置爲clickable = false。有沒有辦法讓事件發生?

回答

0

如果你真的不想讓按鈕不可點擊,你可以添加一個監聽器到按鈕並執行與LinearLayout onClick相同的操作。這對用戶來說似乎是一個大按鈕。

+0

謝謝梅拉,這個工程。我應該如何將LinearLayout onClick事件級聯到ImageButton,以便在LinearLayout被按下時其drawable變化狀態? – Richard 2010-08-26 13:27:00

+0

我不確定你指的是什麼狀態。圖像按鈕是否像切換按鈕一樣,顯示點擊狀態?您應該可以在切換按鈕上調用setChecked(checkedState)。 – 2010-08-26 16:09:00

4

爲了使LinearLayout可點擊,您需要在其所有子元素上設置android:focusable="false"

+1

我設置了所有孩子「android:focusable = false」,但LinearLayout仍然不可觸摸 – 2015-06-11 13:08:06

1

從我個人理解,你會需要以下:

<LinearLayout android:orientation="vertical" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_weight="20" android:gravity="center" 
     android:clickable="true" android:id="@+id/action_showhide"> 
     <ImageButton android:id="@+id/test" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:src="@drawable/ic_toggle_hide_states" android:background="@null">   </ImageButton> 
     <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="@string/txtHide" 
      android:textColor="@drawable/orange" android:textStyle="bold" 
android:onClick="viewClicked"></TextView> 
    </LinearLayout> 

之後,創建功能:在ImageButton的

public void viewClicked(View v){ 

    switch (v.getId()) 
    { 
     case R.id.action_showhide: 
      //do something 
      break; 
     case R.id.action_showhide: 
      //do something 
      break; 
    } 

} 
0

集機器人::點擊= 「假」 。這應該有所幫助。

+2

不,請閱讀以下問題:「所以我不想將它設置爲clickable = false」 – thriqon 2014-09-19 06:31:04