简体   繁体   中英

Options doesn't show up when hardware menu is pressed

I have 2 devices on which i am testing my application, a Galaxy Nexus and a Desire HD (which has hardware buttons)

I am implementing the menu like this

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.feedback:

                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("message/rfc822");
                i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"test@gmail.com"});
                i.putExtra(Intent.EXTRA_SUBJECT, " feedback");
                i.putExtra(Intent.EXTRA_TEXT   , "");
                try {
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }
                return true;
            case R.id.about:
                Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return true;
        }
    }

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

On the galaxy nexus the action bar menu button shows up, and it inflates the menu and everything works fine. On the Desire HD in the action bar the menu button doesn't show up because i have hardware buttons, but if i press the hardware menu button nothing happens.

how can i fix this?

EDIT: this is my xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/feedback"
          android:icon="@drawable/ic_launcher"
          android:title="Feedback"
          android:showAsAction="never"/>
    <item android:id="@+id/about"
          android:icon="@drawable/ic_launcher"
          android:title="About" />
</menu>

Can i hardcode it to do the default action?

I'm still not sure why this is happening, but here is how to intercept the Menu button and open the Menu.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_MENU) {
        openOptionsMenu();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM