如何让菜单项响应命令?

How to get menu items to respond to commands?

我在操作栏上有我的菜单,但最近切换到一个更可自定义的工具栏,并在那里添加了相同的菜单。当菜单附加到操作栏时,菜单中的后退、前进和共享项目工作正常,但现在它们在工具栏上根本不起作用。菜单下拉并完美显示项目,但它们没有响应,只是在单击时关闭菜单,而不是执行所需的操作并关闭菜单。我修改了代码并尝试了几种不同的方法,但没有任何效果。关于如何使它正常工作,有什么想法吗?

这是在主要操作栏上工作的代码 java:

 toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setSubtitle("Test Subtititle");
    toolbar.inflateMenu(R.menu.my_menu);

}



@Override
    public boolean onCreateOptionsMenu (Menu menu){
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.my_menu, menu);
    return super.onCreateOptionsMenu(menu);

}

    @Override
    public boolean onOptionsItemSelected (MenuItem item){
    switch (item.getItemId()) {

        case R.id.menu_back:
            onBackPressed();
            break;

        case R.id.menu_forward:
            onForwardPressed();
            break;

        case R.id.menu_share:
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(Intent.EXTRA_TEXT, myCurrentUrl);
            shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Copied URL");
            startActivity(Intent.createChooser(shareIntent, "Share..."));

         }
        return super.onOptionsItemSelected(item);

    }


    private void onForwardPressed () {
    if (SearchWebAddress.canGoForward()) {
        SearchWebAddress.goForward();
    } else {
        Toast.makeText(this, "Can't go forward!", Toast.LENGTH_SHORT).show();
    }

}

这是我试过的其他方法

  toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setSubtitle("Test Subtititle");
    toolbar.inflateMenu(R.menu.my_menu);

    }

    @Override
    public boolean onCreateOptionsMenu (Menu menu){
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.my_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected (MenuItem item){
        switch (item.getItemId()) {
            case R.id.menu_back:
                onBackPressed();
                return true;
            case R.id.menu_forward:
                onForwardPressed();
                return true;
            case R.id.menu_share:
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_TEXT, myCurrentUrl);
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Copied URL");
                startActivity(Intent.createChooser(shareIntent, "Share..."));
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    private void onForwardPressed () {
        if (SearchWebAddress.canGoForward()) {
            SearchWebAddress.goForward();
        } else {
            Toast.makeText(this, "Can't go forward!", Toast.LENGTH_SHORT).show();
        }
    }

这是主要的xml

  <?xml version="1.0" encoding="utf-8"?>


    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mylayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        tools:context="com.gmail55cyrjj.machvelocitybrowser30.UrlSearch">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="67dp"

            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="-2dp"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="662dp"
            android:background="#DD000000"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize"
            app:titleTextColor="@android:color/white" />

        <Button
            android:id="@+id/go"
            android:layout_width="58dp"
            android:layout_height="36dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="15dp"
            android:layout_marginEnd="48dp"
            android:layout_marginRight="48dp"
            android:background="#8E24AA"
            android:text="Go!"
            android:textSize="16sp" />


        <Button
            android:id="@+id/home_button"
            android:layout_width="63dp"
            android:layout_height="36dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="691dp"
            android:layout_marginEnd="170dp"
            android:layout_marginRight="170dp"
            android:background="@drawable/ic_home_black_18dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <android.support.v7.widget.AppCompatEditText
            android:id="@+id/input_search_url"
            android:layout_width="303dp"
            android:layout_height="35dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="11dp"
            android:layout_marginLeft="11dp"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="8dp"
            android:autofillHints=""
            android:background="#0000D1"
            android:ems="10"
            android:gravity="start|center"
            android:hint="Search or Enter Url"
            android:importantForAutofill="auto"
            android:inputType="textUri"
            android:selectAllOnFocus="true"
            android:textColorHighlight="#39ff14"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/home_button"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />


        <TextView
            android:id="@+id/textProgress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="-32dp"
            android:layout_marginEnd="405dp"
            android:layout_marginRight="405dp"
            android:linksClickable="true"
            android:longClickable="true"
            android:scrollHorizontally="true"
            android:textSize="24sp"
            android:visibility="invisible" />


        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe"
            android:layout_width="match_parent"
            android:layout_height="644dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="-1dp"
            android:layout_marginTop="62dp">


            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="640dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="51dp"
                android:forceHasOverlappingRendering="true"
                android:linksClickable="true"
                android:longClickable="true"
                android:scrollHorizontally="true" />


        </android.support.v4.widget.SwipeRefreshLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/the_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/webView"
            android:layout_alignBottom="@+id/home_button"
            android:layout_alignParentStart="true"
            android:layout_marginStart="-5dp"
            android:layout_marginTop="57dp"
            android:layout_marginBottom="-4dp"
            android:background="#ffbb00"
            android:minHeight="?attr/actionBarSize">

        </android.support.v7.widget.Toolbar>

        <ProgressBar
            android:id="@+id/anotherProgressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="0dp"
            android:layout_marginTop="53dp" />


    </RelativeLayout>

这是xml

中的菜单
  <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">


        <item
            android:id="@+id/menu_back"
            android:icon="@drawable/ic_keyboard_arrow_left_black_24dp"
            app:showAsAction="never"
            android:title="Back" />

        <item
            android:id="@+id/menu_forward"
            android:icon="@drawable/ic_keyboard_arrow_right_black_24dp"
            android:title="Forward" />



        <item
            android:id="@+id/menu_share"
            app:showAsAction="never"
            android:icon="@drawable/ic_share_black_24dp"
            android:title="Share" />

        <item android:title="Item" />

    </menu>

onCreateOptionsMenu()onOptionsItemSelected() 仅与操作栏菜单关联。如果您单独使用 Toolbar,那么您可以使用 inflateMenu() 创建菜单(就像您已经做的那样),并且您需要添加一个 Toolbar.OnMenuItemClickListener 来处理点击事件:

toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setSubtitle("Test Subtititle");
toolbar.inflateMenu(R.menu.my_menu);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
    @Override
    public boolean  onMenuItemClick(MenuItem item){
        switch (item.getItemId()) {
            case R.id.menu_back:
                onBackPressed();
                return true;
            case R.id.menu_forward:
                onForwardPressed();
                return true;
            case R.id.menu_share:
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_TEXT, myCurrentUrl);
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Copied URL");
                startActivity(Intent.createChooser(shareIntent, "Share..."));
                return true;
            default:
                return false;
        }
    }
});