我有一個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。有沒有辦法讓事件發生?
謝謝梅拉,這個工程。我應該如何將LinearLayout onClick事件級聯到ImageButton,以便在LinearLayout被按下時其drawable變化狀態? – Richard 2010-08-26 13:27:00
我不確定你指的是什麼狀態。圖像按鈕是否像切換按鈕一樣,顯示點擊狀態?您應該可以在切換按鈕上調用setChecked(checkedState)。 – 2010-08-26 16:09:00