onOptionsSelected 根本不起作用
我做了这个菜单,当我单击菜单项时没有任何反应,它甚至没有显示默认的吐司!马安不知道怎么回事!
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menuLapiz" android:alphabeticShortcut="l"
android:icon="@drawable/pencil" />
<item android:id="@+id/menuCirculo" android:alphabeticShortcut="c"
android:icon="@drawable/circulo" />
<item android:id="@+id/menuTriangulo" android:alphabeticShortcut="t"
android:icon="@drawable/triangulo" />
<item android:id="@+id/menuCuadrado" android:alphabeticShortcut="s"
android:icon="@drawable/cuadrado" /> </menu>
绘制活动.java ...这里我得到了菜单 onOptionsSelected ,它显然不起作用
import android.app.Activity
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast; `
public class DrawActivity extends Activity {
DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// No Title
requestWindowFeature(Window.FEATURE_NO_TITLE);
drawView = new DrawView(this);
setContentView(drawView);
drawView.requestFocus();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater awesome = getMenuInflater();
awesome.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menuLapiz:
Toast.makeText(this, "This is a toast", Toast.LENGTH_LONG);
return true;
default:
Toast.makeText(this, "This is a toast", Toast.LENGTH_LONG);
return true;
}
}}
我不知道为什么 onOptionsItemSelected(MenuItem item) 方法不起作用我已经在没有选项 S 的情况下尝试过,因为我在其他帖子中读到了该建议,但它不起作用。
我是 Android 编程新手,所以我真的不知道发生了什么。任何帮助将不胜感激,非常感谢! pd:如果您想查看 DrawView.java 类,请告诉我
I did this menu and when I click a menu item nothing happens, it doesnt't even shows the default toast! Maan I dont know what is going on!
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menuLapiz" android:alphabeticShortcut="l"
android:icon="@drawable/pencil" />
<item android:id="@+id/menuCirculo" android:alphabeticShortcut="c"
android:icon="@drawable/circulo" />
<item android:id="@+id/menuTriangulo" android:alphabeticShortcut="t"
android:icon="@drawable/triangulo" />
<item android:id="@+id/menuCuadrado" android:alphabeticShortcut="s"
android:icon="@drawable/cuadrado" /> </menu>
DrawActivity.java
... Here I got the menu onOptionsSelected which appereantly is not working
import android.app.Activity
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast; `
public class DrawActivity extends Activity {
DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// No Title
requestWindowFeature(Window.FEATURE_NO_TITLE);
drawView = new DrawView(this);
setContentView(drawView);
drawView.requestFocus();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater awesome = getMenuInflater();
awesome.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menuLapiz:
Toast.makeText(this, "This is a toast", Toast.LENGTH_LONG);
return true;
default:
Toast.makeText(this, "This is a toast", Toast.LENGTH_LONG);
return true;
}
}}
I have no idea why the onOptionsItemSelected(MenuItem item) method is not working I already tried without the S of Options because I read that suggestion in other post but it did not work.
I'm new to android programming so I really have no clue of what is going on .. any help will be appreciated, thanks a lot!! pd: let me know if you want to see the DrawView.java class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在每个案例的末尾添加
return true;
编辑:toast 代码
Add a
return true;
to the end of each caseEDIT: toast code