按下选项按钮时如何调用 Toast

How to call a Toast when option button is pressed

这里的选项按钮是指大多数 android 设备中的硬按钮,它的图像是

我可以通过这种方式按下后退按钮:-

@Override
public void onBackPressed(){
    Toast.makeText(MainActivity.this,"Back button is pressed",Toast.LENGTH_SHORT).show();
    return;
}

您指的是选项按钮,但您编写的代码在后退按钮上被调用。你应该这样做

@Override
public boolean onKeyDown(int keycode, KeyEvent e) {
    switch(keycode) {
        case KeyEvent.KEYCODE_MENU:
            Toast.makeText(MainActivity.this,"Back button is pressed",Toast.LENGTH_SHORT).show();
            return true; //prevent this event from being propagated further
    }
    return super.onKeyDown(keycode, e);
}