为什么我创建的菜单在使用新意图时不起作用

huangapple go评论72阅读模式
英文:

Why is the menu I created not working with a new Intent

问题

我是安卓开发的新手。

我创建了一个 MenuItem,在点击时启动一个新的活动(SettingsActivity)。我没有错误,一切都正常工作,只是当我点击创建的菜单时,它什么也不做。

public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.visualizer_menu, menu);
    return true;
}

public boolean onOptionSelected(MenuItem item){
    int itemThatWasClicked = item.getItemId();

    if (itemThatWasClicked == R.id.action_settings){
        Intent settingsActivityIntent = new Intent(this, SettingsActivity.class);// 使用显式 intent 打开新活动
        startActivity(settingsActivityIntent);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

我尝试在 onOptionSelected() 中添加了一个 Toast,但没有作用,菜单没有响应。

现在我在 SettingsActivity.java 中有以下代码:

package android.example.com.visualizerpreferences;

import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        ActionBar actionBar = this.getSupportActionBar();

        if (actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    public boolean onOptionsItemSelected(MenuItem item){
        int id = item.getItemId();
        if(id == android.R.id.home){
            NavUtils.navigateUpFromSameTask(this);
        }
        return super.onOptionsItemSelected(item);
    }

}

以下是我的菜单资源文件 visualizer_menu.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/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never"/>
</menu>
英文:

I am new to android development.

I created a MenuItem to start a new activity (SettingsActivity) when clicked. I have no errors and everything is working fine except that when I click the created menu, it does not do anything.

public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.visualizer_menu, menu);
        return true;
    }

 public boolean onOptionSelected(MenuItem item){
        int itemThatWasClicked = item.getItemId();

        if (itemThatWasClicked == R.id.action_settings){
            Intent settingsActivityIntent = new Intent(this, SettingsActivity.class);// using explicity intent to open new activity
            startActivity(settingsActivityIntent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

I have tried adding a Toast in onOptionSelected(), it didn't work. the menu is not just responding

I have the following code in my SettingsActivity.java for now

package android.example.com.visualizerpreferences;

import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        ActionBar actionBar = this.getSupportActionBar();

        if (actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    public boolean onOptionsItemSelected(MenuItem item){
        int id = item.getItemId();
        if(id == android.R.id.home){
            NavUtils.navigateUpFromSameTask(this);
        }
        return super.onOptionsItemSelected(item);
    }

}

Below is my visualizer_menu.xml in my menu resource file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;

    &lt;item
        android:id=&quot;@+id/action_settings&quot;
        android:title=&quot;@string/action_settings&quot;
        android:orderInCategory=&quot;100&quot;
        app:showAsAction=&quot;never&quot;/&gt;
&lt;/menu&gt;

答案1

得分: 1

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
英文:

replace your

public boolean onOptionSelected(MenuItem item){

to

@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

答案2

得分: 0

菜单项创建

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.game_menu, menu);
        return true;
    }

点击监听器

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // 处理选项选择
        switch (item.getItemId()) {
        case R.id.new_game:
            newGame();
            return true;
        case R.id.help:
            showHelp();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
英文:

Menu item create :

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

Click listeners :

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.new_game:
        newGame();
        return true;
    case R.id.help:
        showHelp();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

huangapple
  • 本文由 发表于 2020年10月5日 20:42:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64208834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定