为什么点击 optionsMenu 项不起作用?

Why click on optionsMenu item doesn't work?

单击选项菜单中的项目时出现问题。我已经阅读了如何在网站 https://developer.android.com/guide/topics/ui/menus 上创建菜单,我似乎做对了,但它仍然不起作用。菜单存在,它是可见的,但是当我执行点击时没有任何反应。此外,Logcat.

中没有错误

菜单的 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:title="@string/edit"
        android:id="@+id/edit_menu_script_item"
        app:showAsAction="always"
        android:icon="@drawable/ic_edit"/>
    <item android:title="@string/delete"
        android:id="@+id/delete_menu_script_item"
        app:showAsAction="never"/>
    <item android:title="@string/share"
        android:id="@+id/share_menu_script_item"
        app:showAsAction="never"/>
    <item android:title="@string/scenes"
        android:id="@+id/scenes_menu_script_item"
        app:showAsAction="never"/>
    <item android:title="@string/characters"
        android:id="@+id/characters_menu_script_item"
        app:showAsAction="never"/>

</menu>

编辑:Toasts 消息均未显示,菜单在执行单击项目后关闭。

该菜单在工具栏上有一个项目,在下拉菜单中有其他项目:

根据要求,这是我的 activity 的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ViewProjectSW">

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbarViewProjectSW"
        app:title="@string/view_your_script"
        app:menu="@menu/menu_script"
        android:background="@color/barColor"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/textBodyColor"
        android:hint="@string/script_here"
        android:id="@+id/scriptTV"/>
</LinearLayout>

和 java class:

package com.stud.scriptreality;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.stud.scriptreality.classes.Screenwriter;
import com.stud.scriptreality.classes.Script;

public class ViewProjectSW extends AppCompatActivity {

    Toolbar toolbar;
    TextView scriptTV;
    Intent intent;
    String title;
    Script script;
    Screenwriter screenwriter = new Screenwriter();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_project_sw);

        initialization();

    }

    public void initialization(){
        intent=getIntent();
        title=intent.getStringExtra("title");
        int id= intent.getIntExtra("position",0);
        screenwriter = intent.getParcelableExtra("autor");

        toolbar=findViewById(R.id.toolbarViewProjectSW);
        toolbar.setTitle(title);
        script = new Script(id,screenwriter,title);

        scriptTV=findViewById(R.id.scriptTV);
        scriptTV.setClickable(false);
        Toast.makeText(this, R.menu.menu_script+"*", Toast.LENGTH_SHORT).show();
    }

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

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        Toast.makeText(this, item.getItemId(), Toast.LENGTH_SHORT).show();

        switch (item.getItemId()){
            case R.id.edit_menu_script_item:
                Toast.makeText(this, "Opening edit page...", Toast.LENGTH_SHORT).show();
                Intent it = new Intent(getApplicationContext(),EditScript.class);
                it.putExtra("script",script);
                startActivity(it);
                break;
            case R.id.characters_menu_script_item:
                Toast.makeText(this, "Opening characters list...", Toast.LENGTH_SHORT).show();
                Intent it2 = new Intent(getApplicationContext(),ViewCharactersSW.class);
                startActivity(it2);
                break;
            case R.id.delete_menu_script_item:
                Toast.makeText(this, "Deleting script...", Toast.LENGTH_SHORT).show();
                break;
            case R.id.scenes_menu_script_item:
                Toast.makeText(this, "Opening scenes list...", Toast.LENGTH_SHORT).show();
                break;
            case R.id.share_menu_script_item:
                Toast.makeText(this, "Opening sharing dialog...", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

}

我不使用 ActionBar,而是我创建的另一个工具栏,我为整个应用程序设置了一个带有 NoActionBar 的样式。 <style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">

提前致谢!

试试这个代码。

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
        switch(item.getItemId()){ 
            case R.id.delete_menu_script_item: 
                Toast.makeText(this, "Deleting script...", Toast.LENGTH_SHORT).show();
                break; 
            case R.id.characters_menu_script_item:
                Toast.makeText(this, "Opening characters list...", Toast.LENGTH_SHORT).show();
                break; 
        } 
        return super.onOptionsItemSelected(item); 
    }

尝试在您的初始化方法中添加这一行:

setSupportActionBar()

嗯,你需要设置支持操作栏setSupportActionBar();并传递您的工具栏变量,如下所示:

setSupportActionBar(工具栏);