[英]Why click on optionsMenu item doesn't work?
I have a problem when clicking on an item from an options menu.单击选项菜单中的项目时出现问题。 I've read how to create a menu on the website https://developer.android.com/guide/topics/ui/menus and I seem to do it right, but it still doesn't work.
我已经阅读了如何在https://developer.android.com/guide/topics/ui/menus网站上创建菜单的方法,我似乎做得对,但它仍然不起作用。 The menu exists, it is visible, but nothing happens when I perform the click.
菜单存在,它是可见的,但是当我执行点击时没有任何反应。 Also, there are no errors in the Logcat.
此外,Logcat 中没有错误。
The XML file for the menu is:菜单的 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>
EDIT: Neither of the Toasts messages are displayed and the menu closes after performing click on an item.编辑:没有显示 Toasts 消息,并且在执行单击项目后菜单关闭。
The menu has one item on the toolbar, and the others in the dropdown menu:该菜单在工具栏上有一项,其他在下拉菜单中:
As requested, here is the XML code for my 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>
and the java class:和 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;
}
}
I don't use the ActionBar, but another Toolbar I created, and I had set for the entire app a style with NoActionBar.我不使用 ActionBar,而是我创建的另一个 Toolbar,并且我为整个应用程序设置了一个带有 NoActionBar 的样式。
<style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">
Thanks in advance!提前致谢!
Try this code.试试这个代码。
@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);
}
Try add this line in your initialization method:尝试在初始化方法中添加这一行:
setSupportActionBar() setSupportActionBar()
Well, you need to set support action bar setSupportActionBar();那么,你需要设置支持动作栏 setSupportActionBar(); and pass your toolbar variable, like this:
并传递您的工具栏变量,如下所示:
setSupportActionBar(toolbar); setSupportActionBar(工具栏);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.