Android 硬件菜单按钮代码无法正常工作!需要帮助
我喜欢这个网站,你们都很棒!但这是我遇到的另一个问题: 在我的应用程序中,我有一个在整个屏幕上显示网站的网络视图,我编写了一个代码,通过按下手机的菜单按钮来显示菜单,我希望发生两件事第一个菜单项返回到应用程序的主屏幕,第二个菜单项退出应用程序或退出应用程序。 第一个问题: 按菜单按钮后,它会显示菜单...如果我再次按它,它会显示两个选项两次,如果我再次按它,现在两个项目都会显示 3 次,依此类推! 第二个问题: 选择两个选项中的任何一个后,什么都不会发生! 这是我的代码请告诉我我做错了什么! 感谢
菜单 xml:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menuToMenu"
android:title="Menu Principal"
/>
<item
android:id="@+id/menuToSalir"
android:title="Salir"
/>
</menu>
Backtomain.java
import android.app.Activity;
import android.os.Bundle;
public class Backtomain extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
以及我调用菜单的位置:
public boolean onPrepareOptionsMenu (Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mostrar = getMenuInflater();
mostrar.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuToMenu:
startActivity (new Intent("my.app.BACKTOMAIN"));
return true;
case R.id.menuToSalir:
finish();
System.exit(0);
return true;
}
return false;
}
I love this site all you guys are awesome! but here is another problem I have:
In my app I have a webview that displays a website in the entire screen, I have made a code to show a menu through pushing the phone's menu button from where i want 2 things to happen 1st menu item Go back to main screen of the app, 2nd menu item quit the app or exit the app.
First problem:
after pressing the menu button it shows the menu... if I press it again it shows the two choices twice, if I press it again now both items shows 3 times and so on!
Second Problem:
after choosing any of the two choices nothing happens!
here is my code please tell me what I'm doing wrong!
Thanks
menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menuToMenu"
android:title="Menu Principal"
/>
<item
android:id="@+id/menuToSalir"
android:title="Salir"
/>
</menu>
Backtomain.java
import android.app.Activity;
import android.os.Bundle;
public class Backtomain extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
and where I call the menu:
public boolean onPrepareOptionsMenu (Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mostrar = getMenuInflater();
mostrar.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuToMenu:
startActivity (new Intent("my.app.BACKTOMAIN"));
return true;
case R.id.menuToSalir:
finish();
System.exit(0);
return true;
}
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在从
onPrepareOptionsMenu()
调用super.onCreateOptionsMenu()
。并且,您将相同的选项添加到onPrepareOptionsMenu()
中的菜单中。将onPrepareOptionsMenu()
重命名为onCreateOptionsMenu()
,它可能会表现得更好。另外:
my.app.BACKTOMAIN
活动正在运行,您可能需要将FLAG_ACTIVITY_REORDER_TO_FRONT
或FLAG_ACTIVITY_CLEAR_TOP
添加到Intent
。menuToSalir
菜单选项。编写良好的 Android 应用程序不会调用System.exit(0)
。用户通过按 HOME 按钮离开您的应用程序,这与在 Web 应用程序中没有什么不同。You are calling
super.onCreateOptionsMenu()
fromonPrepareOptionsMenu()
. And, you are inflating the same options into the menu inonPrepareOptionsMenu()
. RenameonPrepareOptionsMenu()
toonCreateOptionsMenu()
, and it will probably behave better.Also:
my.app.BACKTOMAIN
activity is running, you probably want to addFLAG_ACTIVITY_REORDER_TO_FRONT
orFLAG_ACTIVITY_CLEAR_TOP
to theIntent
.menuToSalir
menu choice. No well-written Android application will callSystem.exit(0)
. Users leave your application by pressing the HOME button, no different than they might in a Web app.要解决您的第一个问题,请尝试 onCreateOptionsMenu(),而不是 onPrepareOptionsMenu()。
我不确定如何解决第二个问题,我通常全部用 java 创建菜单,而不是像您一样使用 xml。
To fix your first problem try onCreateOptionsMenu(), rather than onPrepareOptionsMenu().
I am not positive how to fix second problem, I usually create my menus all in java instead of using xml like you have.
我发现了问题,它有效!我错过了“s”...
正确的方法是
感谢您的帮助 Tim 和 CommonsWare
I found the problem it works! I was missing the "s" at...
the right way is
thanks for your help Tim and CommonsWare
我遇到了这个问题。就我而言,我已将活动屏幕的背景颜色设置为黑色。当菜单弹出时,它有透明背景和黑色文本,所以我没有看到它工作。
I ran into this problem. In my case I had set the background color of the activity screen to black. When the menu popped up it had a transparent background and black text so I didn't see it working.