0

我做了这个菜单,当我点击一个菜单项时没有任何反应,它甚至没有显示默认的吐司!马云不知道怎么回事!

<?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 ...在这里我得到了菜单 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 类,请告诉我

4

1 回答 1

0

return true;在每个案例的末尾添加一个

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.mAdd:
        startActivity(new Intent("com.example.ADD"));
        return true;

    case R.id.mAbout:
        startActivity(new Intent("com.example.ABOUT"));
        return true;

    }
    return false;
}

编辑:吐司代码

Toast t = Toast.makeText(getApplicationContext(),
                    "This is a toast",
                    Toast.LENGTH_SHORT);
            t.show();
于 2011-12-25T17:45:50.713 回答