【发布时间】:2012-06-24 07:46:30
【问题描述】:
我有一个使用 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);
}
【问题讨论】:
-
尝试使 ImageView 不可点击(将属性设置为 false)。这有帮助吗?
-
即使它对你有用......你仍然应该像 David Caunt 建议的那样使用
onOptionsItemSelected()。
标签: android actionbarsherlock android-actionbar