菜单活动不起作用
我的应用程序主屏幕上有几个按钮和列表视图。我想在其中添加菜单活动。所以我只是编写了自定义菜单活动并从主屏幕扩展。但不幸的是,当我单击手机上的菜单按钮时,我的自定义菜单不起作用。有什么想法吗?
package com.gokiri.gl.activity;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.gokiri.gl.R;
import com.gokiri.gl.screen.GLandMainMenu;
import com.gokiri.gl.screen.SearchScreen;
import com.gokiri.gl.util.Shared;
public class GLandMainActivity extends ListActivity {
protected Activity act;
protected int screenWidth, screenHeight;
protected Bitmap bannerBitmap;
protected void initActivity(Activity act) {
this.act = act;
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
dm = null;
Shared.addAcitivity(act);
setDefaultButton();
}
public void setDefaultButton() {
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
Shared.removeAcitivity();
startActivity(new Intent(this, GLandMainMenu.class));
return true;
}
return super.onKeyDown(keyCode, event);
}
private Menu mMenu;
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home:
// act.finish();
Intent main = new Intent(act, GLandMainMenu.class);
startActivity(main);
return true;
case R.id.menu_search:
act.finish();
Intent search = new Intent(act, SearchScreen.class);
startActivity(search);
return true;
case R.id.menu_bookmarks:
return true;
case R.id.menu_about:
return true;
}
return super.onMenuItemSelected(featureId, item);
}
}
从评论中编辑:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_home"
android:title="@string/home"
android:icon="@drawable/tab_home" />
<item android:id="@+id/menu_search"
android:title="@string/search"
android:icon="@drawable/tab_search" />
<item android:id="@+id/menu_bookmarks"
android:title="@string/bookmark"
android:icon="@drawable/tab_about" />
<item android:id="@+id/menu_about"
android:title="@string/about"
android:icon="@drawable/tab_about" />
</menu>
I have several buttons and list view at my app home screen. And I want to put menu activity at it. So I just wrote my custom menu activity and extends from home screen. But unfortunately that my custom menu doesn't working when I clicked menu button at my phone. Any ideas ?
package com.gokiri.gl.activity;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.gokiri.gl.R;
import com.gokiri.gl.screen.GLandMainMenu;
import com.gokiri.gl.screen.SearchScreen;
import com.gokiri.gl.util.Shared;
public class GLandMainActivity extends ListActivity {
protected Activity act;
protected int screenWidth, screenHeight;
protected Bitmap bannerBitmap;
protected void initActivity(Activity act) {
this.act = act;
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
dm = null;
Shared.addAcitivity(act);
setDefaultButton();
}
public void setDefaultButton() {
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
Shared.removeAcitivity();
startActivity(new Intent(this, GLandMainMenu.class));
return true;
}
return super.onKeyDown(keyCode, event);
}
private Menu mMenu;
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home:
// act.finish();
Intent main = new Intent(act, GLandMainMenu.class);
startActivity(main);
return true;
case R.id.menu_search:
act.finish();
Intent search = new Intent(act, SearchScreen.class);
startActivity(search);
return true;
case R.id.menu_bookmarks:
return true;
case R.id.menu_about:
return true;
}
return super.onMenuItemSelected(featureId, item);
}
}
EDIT from comment:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_home"
android:title="@string/home"
android:icon="@drawable/tab_home" />
<item android:id="@+id/menu_search"
android:title="@string/search"
android:icon="@drawable/tab_search" />
<item android:id="@+id/menu_bookmarks"
android:title="@string/bookmark"
android:icon="@drawable/tab_about" />
<item android:id="@+id/menu_about"
android:title="@string/about"
android:icon="@drawable/tab_about" />
</menu>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您用拦截 KeyEvent.KEYCODE_MENU 的 onKeyDown 来阻止 onCreateOptionMenu() 。您不能使用菜单按钮来启动活动和显示菜单(显然)。如果不是那个,请向我们解释什么是 GLandMainMenu 类
For what i see , you blocking the onCreateOptionMenu() with the onKeyDown who intercept the KeyEvent.KEYCODE_MENU. You can't use the menu button for both launching activity and displaying menu (obviously). And if this is not that , explain us what's the GLandMainMenu class