Android 自定义工具栏 onOptionsItemSelected 不工作

标签 android android-actionbar onclicklistener toolbar

我有两个相似的按钮

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
    android:id="@+id/sync_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sync"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    />

</RelativeLayout>

作为项目添加到菜单

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/toggle_item"
    android:title=""
    app:showAsAction="always"
    app:actionLayout="@layout/switch_button"
    />
<item
android:id="@+id/sync_item"
android:title=""
app:showAsAction="always"
app:actionLayout="@layout/sync"
/>
</menu>

我使用

在 onCreateOptionsMenu 中将项目膨胀到菜单
getMenuInflater().inflate(R.menu.menu_main, menu);

一切似乎都很好,但是当我点击应用程序时,没有任何反应,因为永远不会调用 onOptionsItemSelected(MenuItem item)

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Integer test = item.getItemId();
    switch (item.getItemId()) {
        case R.id.toggle_item:
          ...
        case R.id.sync_item:
          ...
  return super.onOptionsItemSelected(item);
 }

添加工具栏

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

最佳答案

我终于找到了答案。这两个答案的结合解决了我的问题:

https://stackoverflow.com/a/17764895/2925656

https://stackoverflow.com/a/23936117/2925656

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    final Menu m = menu;
    final MenuItem item = menu.findItem(R.id.sync_button);
    item.getActionView().setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {   
            do_stuff;
        }
    });
    return true;
}

菜单项:

  <item
    android:id="@+id/myswitch"
    android:title=""
    app:showAsAction="always"
    app:actionLayout="@layout/toggle" />

Activity 布局必须实现:

android:clickable="false"

我的切换 Action 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false">
<ToggleButton
        android:id="@+id/actionbar_service_toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="Discovery"
    android:textOff="Favourite"
    android:clickable="false"/>
</RelativeLayout>

关于Android 自定义工具栏 onOptionsItemSelected 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35258209/

相关文章:

android - 更改 android 操作栏的背景颜色,基本教程

android - 自定义按钮的 ClickListener

android - 如何通过解析json数据动态添加table view

Android KSOAP2 SSL java.security.cert.CertPathValidatorException

android - 同一个服务器代码能否同时向 APNS 和 GCM 发送通知

java - 我正在努力添加一个操作栏

android - 如何动态更改 ActionBar 菜单项的图标

android - Android 中的继承

android - 一个按钮的多个onClickListener

android - 无法在水平 RecyclerView 中的项目之间添加边距