这是我的代码:
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但我遇到了一些错误。
有人可以将此添加到我的代码中并在此处发布完整代码,这样我就可以看到我做错了什么?
提前谢谢
答案 0 :(得分:3)
您可以参考这个问题:
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);
}
答案 1 :(得分:0)
您必须将此代码添加到您的活动中:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME)
{
return false;
}
return super.onKeyDown(keyCode, event);
}
告诉我它是否有效;)