如何操作“活动"的“选项"菜单 [英] How to manipulate the Activity's Options Menu

查看:54
本文介绍了如何操作“活动"的“选项"菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Google上搜索了,没有找到有关如何操作设置按钮的功能或当按下手机上的设置触摸按钮时会显示的内容的代码.

I've Googled and come up with no code on how to manipulate what the settings button does, or what is brought up when the settings touch button on the phone is pressed.

以下是我的意思的屏幕截图:

Here is a screenshot of what I mean:

在底部,您可以看到它显示设置,但是当我单击它时,什么也没有发生.我想更改单击它时会发生什么.

At the bottom, you can see that it says settings, but when I click on it, nothing happens. I want to change what happens when it is clicked.

此外,我希望能够在按下实际电话上的设置按钮时更改所显示的内容(屏幕截图中的设置选项为所显示的内容).例如.在S3上,它是设备左下方的触摸按钮.

Also, I want to be able to change what is brought up (the settings option in the screenshot is what's brought up) when the settings button on the actual phone is pressed. eg. on the S3, it's the touch button at the bottom left of your device.

任何人都可以将我链接到资源,或者写一些示例代码来说明如何做到这两者,甚至是我想做的一件事情吗?

Can anyone link me to a resource, or write some sample code for how to do both, or even one of the things I want to do?

推荐答案

您需要覆盖Activity的 onOptionsItemSelected(...)方法.

You need to override your Activity's onOptionsItemSelected(...) method.

这样做:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == R.id.action_settings) { // or whatever your id is, see the .xml file
        // do something

        return true;

    } else {
        return super.onOptionsItemSelected(item);
    }
}

检查选项项的ID (例如设置),并在if内做特定的事情.

Check the id of the options item (e.g. settings) and do specific stuff inside the if.

为了添加更多项目或操纵ID,请查看您的项目菜单"文件夹及其中的.xml文件.

In order to add more items or manipulate the Id, take a look at your projects "menu" folder and the .xml file inside it.

这是我的主菜单中的.xml文件:

Here the .xml file of my main menu:

及其内部外观:

(您可以操纵该文件并向其添加更多操作,该操作将显示在菜单中)

(you can manipulate this file and add more actions to it that will show up in the menu)

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/action_one" android:title="Action 1"></item>
    <item android:id="@+id/action_two" android:title="Action 2"></item>
    <item android:id="@+id/action_three" android:title="Action 3"></item>

</menu>

在这种情况下,我的菜单将包含3个动作,您应该在 onOptionsItemSelected(...)方法中检查它们,如下所示:

In this case my menu would have 3 Actions, and you should check for them inside your onOptionsItemSelected(...) method like that:

 if (item.getItemId() == R.id.action_one) //...

...或使用开关盒.有关更详细的说明,您也可以查看本教程:

... or use switch-case. For a more detailed explanation, you can also have a look at this tutorial:

http://mobile.tutsplus.com/tutorials/android/android-sdk-implement-an-options-menu/

这篇关于如何操作“活动"的“选项"菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆