2012-01-17 11 views
1

私は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); 
} 
+0

ImageViewをunclickableにする(属性をfalseに設定する)ようにしてください。それは役に立ちますか? –

+0

でも、あなたのために働いています...あなたはDavid Cauntが示唆しているように 'onOptionsItemSelected()'を使うべきです。 –

答えて

3

ここに私の提案された修正があります。別のactionLayoutの必要はありません。あなたが望む結果はそのままでは簡単です。 ActionBarの "ImageView"を作成する必要はありません... android:iconという属性セットが私たちのために用意されています。 android:titleを使用して、アイコンのテキストを設定します。

menu_watchlist.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:id="@+id/image_button" 
      android:showAsAction="always|withText"   
      android:title="title" 
      android:icon="@drawable/button_with_states" />  
</menu> 

セットアップオプションメニュー:これはあなたのために働く

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_watchlist, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.image_button: 
      Log.d("Test", "Hello"); 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 

願っています!

+0

乾杯。それは魅力のように働いた。 – nicktmro

+0

:) –

1

ABSまたはネイティブシステムのActionBarでActionViewを押すと、onOptionsItemSelectedメソッドが呼び出されます。イベントリスナーを手動でアタッチする必要はありません。実際には、ではなく、が一貫して動作します。 ActionItemの選択をリッスンするために

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.menu_save: 
      //Save was pressed, call a method 
      break; 
     case R.id.menu_another_item: 
      //A different item was pressed 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 

onOptionsItemSelectedの使用は少し反直感的であるが、onCreateOptionsMenuがするように、メニューを使用しての日に戻っ出身。

+0

ありがとうございます、あなたの答えは有用ですが、本当に問題を解決しませんでした。アンドロイド:アイコンを使用するアレックスの提案は、私が必要としたものでした。 – nicktmro

+0

AFAICT(2012年10月現在)、メニュー項目のカスタムレイアウトを設定する場合は、明示的にイベントリスナーをアタッチする必要があります。新しいビューのクリックに対して、onOptionItemSelectedは自動的に起動しません。 –

+0

@david caunt case R.id.menu_save:私にコンパイル時のエラーを与える:case式は定数でなければならない。 – test

関連する問題