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

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

问题描述

我GOOGLE了,并拿出有关如何操作的设置按钮做什么,或者什么是长大当设置触摸手机上的按钮没有code是pressed。

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.

下面是我的意思截图:

在底部,你可以看到,它说的设置,但是当我点击它,什么也没有发生。我想改变它被点击时会发生什么。

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.

另外,我希望能够改变什么长大(截图中的设置选项是什么造就了)时,实际手机的设置按钮是pressed。例如。在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.

任何人都可以联系我到资源,或者写一些样品$ C $下如何做到既,还是我想要做的事情,甚至一个吗?

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?

推荐答案

您需要重写你的活动的 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 (如设置),并做具体的东西在里面,如果。

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

为了添加更多的项目或操纵标识,看看你的项目菜单中的文件夹,里面的.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:

和它的外观里面:

您可以处理此文件,并添加更多的操作它会显示在菜单上)

<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>

在这种情况下,我的菜单将有三个动作,你应该检查他们在你的 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) //...

...或者使用的switch-case。 如需更详细的解释,你也可以看看这个教程:

... 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天全站免登陆