【问题标题】:Android with ActionBar Sherlock. ImageButton does not get onClick event带有 ActionBar Sherlock 的 Android。 ImageButton 没有得到 onClick 事件
【发布时间】: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


【解决方案1】:

这是我建议的修复方法。确实不需要单独的 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);
}

希望这对你有用!

【讨论】:

    【解决方案2】:

    当您在 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 也是如此。

    【讨论】:

    • 谢谢,您的回答很有用,但并没有真正解决问题。 Alex 建议使用 android:icon 正是我所需要的。
    • AFAICT(截至 2012 年 10 月),如果您正在为菜单项设置自定义布局,那么您还必须显式附加事件侦听器。 onOptionItemSelected 不会因为点击新视图而自动触发。
    • @david caunt case R.id.menu_save: 给我一个编译时错误: case expression must be constant
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多