【问题标题】:How to remove menu hardware key from your android app如何从您的 android 应用程序中删除菜单硬件密钥
【发布时间】:2016-04-20 01:16:56
【问题描述】:

我四处寻找,但找不到解决问题的方法。我想从我的应用程序中删除菜单硬件键,这样我就可以在操作栏中让我的菜单溢出

这是我的代码:

package gridview;

import fragments.MainFragment;
import gab.GlassActionBarHelper;
import hd.light.AboutDev;
import hd.light.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class Main extends SherlockFragmentActivity {

    private SharedPreferences prefs;
    private GlassActionBarHelper helper;

    // Starts the Activity for the gridview
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        prefs = getSharedPreferences(getResources().getString(R.string.theme_name), 0);
        checkBuild();

        helper = new GlassActionBarHelper().contentLayout(R.layout.gridview_main);
        setContentView(helper.createView(this));

        getSupportFragmentManager().beginTransaction()
        .replace(R.id.container, new MainFragment())
        .commit();
    }

    public void checkBuild() {
      int buildNum = prefs.getInt("Build Number", 1);
      int currentVersion = 0;

      try {
        currentVersion = getPackageManager()
                .getPackageInfo(getPackageName(), 0).versionCode;
      }
      catch (NameNotFoundException e) {
        e.printStackTrace();
      }
        if(currentVersion > buildNum) {
              getChangelog().show();
              Editor editor = prefs.edit();
              editor.putInt("Build Number", currentVersion);
              editor.commit();
            }
      }

    public Dialog getChangelog()
     {
        final Dialog CDialog = new Dialog(Main.this);
        CDialog.setTitle(getResources().getString(R.string.changelog_title));
        CDialog.setContentView(R.layout.changelog);
        CDialog.setCanceledOnTouchOutside(true);
        CDialog.setCancelable(true);

        Button Close = (Button) CDialog.findViewById(R.id.close);
        Close.setOnClickListener(new View.OnClickListener()
        {
         @Override
         public void onClick(View v)
         {
         CDialog.dismiss();
         }
        });

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

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch(item.getItemId())
        {
            case R.id.shareButton:
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.app_link));
                startActivity(Intent.createChooser(shareIntent, "Share Via"));
                break;
            case R.id.rateButton:
                Intent rate = new Intent(Intent.ACTION_VIEW).setData(Uri.parse
                        ("market://details?id=your.icons.name.here"));
                startActivity(rate);
                break;
            case R.id.emailButton:
                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "the1dynasty.android@gmail.com" });
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getText(R.string.email_subject));
                emailIntent.setType("plain/text");
                startActivity(Intent.createChooser(emailIntent, "Contact Developer"));

                break;
            case R.id.aboutButton:
                Intent about = new Intent(Main.this, AboutDev.class);
                startActivity(about);
                break;
            case R.id.donateButton:
                Intent donate = new Intent(Intent.ACTION_VIEW).setData(Uri.parse
                        ("http://bit.ly/YWwhWu"));
                startActivity(donate);
                break;
        }

        return true;
    }

}

因为我拥有 Galaxy S4,当我按下菜单按钮时,我会从底部弹出。当我在没有菜单按钮的 Nexus 4 模拟器上进行测试时,我得到了想要的结果,操作栏中的菜单溢出(3 个点)。如何从我的应用程序中删除菜单硬件密钥?另外,我按照此处的说明进行操作 ---> How to force use of overflow menu on devices with menu button 但我遇到了一些错误。

能否请一些人将此添加到我的代码中并在此处发布整个代码,以便我可以看到我做错了什么?

提前谢谢你

【问题讨论】:

    标签: android-intent android-actionbar


    【解决方案1】:

    你可以参考这个问题:

    How to force use of overflow menu on devices with menu button

    要禁用硬件菜单键,您可以使用以下代码:

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

    【讨论】:

      【解决方案2】:

      您必须将此代码添加到您的活动中:

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

      告诉我它是否有效;)

      【讨论】:

      • 我一回家就试试。让你知道。非常感谢
      • 不,它不起作用,我在将您的代码添加到我的代码时得到了 FC。还有其他建议吗?
      • 它不起作用,因为我使用的是 ActionBarSherlock。我得先删除它,然后我会尝试
      • 我现在正在工作,我没有空闲时间...如果你可以删除 ActionBarSherlock,我相信这段代码可以工作。
      • 是的,但是移除所有 ABS 需要时间 :(
      猜你喜欢
      • 1970-01-01
      • 2020-03-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多