私はActionBar Sherlockを使用するAndroidアプリを持っています。 Drawableリソースファイルで状態が定義されたImageButtonを持つメニューを作成します。 (それらのすべてが下に貼り付けられています)。アンドロイドActionBarシャーロック。 ImageButtonがonClickイベントを取得しない
ImageButtonの選択/非選択状態を切り替えることはできますが、クリックリスナーは起動しないようです。
アクティビティが作成されると、メニューが膨張し、ImageButtonが取得され、イベントリスナーが登録されます。私はデバッグし、それはすべてOKです(正しいImageButtonにイベントを登録しています)。
あなたはonClickのコールバックを取得しないためにのImageButtonを引き起こす可能性があると思いますか?
乾杯....
ここにいくつかのコードです:
メニュー:
<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をunclickableにする(属性をfalseに設定する)ようにしてください。それは役に立ちますか? –
でも、あなたのために働いています...あなたはDavid Cauntが示唆しているように 'onOptionsItemSelected()'を使うべきです。 –