我有一个问题,当点击一个选项菜单中的项目时。我在网站上读到了如何创建一个菜单 https:/developer.android.comguidetopicsuimenus。 我似乎做对了,但还是不行。菜单是存在的,它是可见的,但当我执行点击时,什么也没有发生。另外,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>
EDIT: Toasts信息都不显示,菜单在点击一个项目后关闭。
按照要求,这里是我的活动的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类:
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,而是创建了另一个Toolbar,我为整个应用程序设置了NoActionBar的样式。<style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">
先谢谢你
试着在你的初始化方法中添加这一行。
setSupportActionBar()
那么,你需要设置支持动作条setSupportActionBar();并传递你的工具栏变量,就像这样。
setSupportActionBar(toolbar)。
试试这段代码。
@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);
}