我有一个使用 ActionBar Sherlock 的 Android 应用程序。我创建了一个具有 ImageButton 的菜单,其状态在可绘制资源文件中定义。(所有这些都粘贴在下面)。
虽然我能够切换 ImageButton 的选定/未选定状态,但单击侦听器似乎没有触发。
创建活动时,我膨胀菜单,获得 ImageButton 并注册事件侦听器。我进行了调试,一切似乎都很好(我正在正确的 ImageButton 上注册事件)。
您认为什么会导致 ImageButton 无法获得 onClick 回调?
干杯....
这是一些代码:
菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/menu_save"
android:showAsAction="always|withText"
android:actionLayout="@layout/menu_my_activity"
/>
</menu>
menu_my_activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_with_states"
android:clickable="true" />
</LinearLayout>
和听众的注册:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = (MenuInflater) this.getMenuInflater();
inflater.inflate(R.menu.menu_watchlist, menu);
menu.getItem(0).getActionView().findViewById(R.id.imageButton).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Test", "Hello");
}
});
return super.onCreateOptionsMenu(menu);
}