通过点击菜单选项项打开上下文菜单 [英] Open context menu by clicking on menu options item

查看:145
本文介绍了通过点击菜单选项项打开上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Android提出申请。
我有我的地方创建一个选项菜单中的活动像下面

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    MenuInflater吹气= getMenuInflater();
    inflater.inflate(R.menu.mymenu,菜单);
    返回true;
}

菜单是从一个XML文件加载:

<?pre> &LT; XML版本=1.0编码=UTF-8&GT;&LT;菜单的xmlns:机器人=htt​​p://schemas.android.com/apk / RES / Android的&GT; &LT;项目的android:标题=项目1机器人:ID =@ + ID / ITEM1/&GT;&LT; /菜单&gt;

当我点击项目1,我用onOptionsItemSelected我的活动点击一样,之后的工作:

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
    案例R.id.item1:
        //这里,我想开一个上下文菜单
        返回true;
    默认情况下:
        返回super.onOptionsItemSelected(项目);
    }
}

所以,当第1项用户点击我想开一个上下文菜单。首先,我不知道是否有可能直接打开上下文菜单,而无需使用屏幕上的像互联网上的几个教程保持位置表现出来。

如果有可能我怎么能打开这样一个上下文菜单?

我想使用 registerForContextMenu() openContextMenu()在我的第1项,但其观点的情况下,我应该把参数?

如果有人要进行的方式的想法,我想知道我怎么要做到这一点。


解决方案

如果你真的想有一个上下文菜单,那么我会做一个长期preSS使用上下文菜单,因为它是设计的Andr​​oid上的给定项。人们习惯了这种和应用程序不应该在不同的。

可选择性的是一个有点复杂。你可以做的是让你的菜单,你风格为对话窗口(你知道有一个褪色的半透明背景)的一个独立的活动加载。这可以通过在manifest.xml文件应用 Theme.Dialog 风格您的活动很容易做到:

 &LT;活动机器人:名字=。activities.TagPopupActivity
            机器人:标签=标记安卓主题=@安卓风格/ Theme.Dialog&GT;
                ...
&LT; /活性GT;

或者你也可以直接创建一个对话框窗口,说明这里 。然后,通过在主列表活动实施相应的点击监听器获取上下文(即列表中单击),然后当上了项目的用户点击,您检索其ID,并将其打包成一个包,你期待你的菜单 -activity风格作为一个弹出对话框。

I use Android to make an application. I have an activity where I create an option menu like below

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mymenu, menu);
    return true;
}

The menu is loaded from an xml file :

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="Item1" android:id="@+id/item1" /></menu>

When I click on item 1, I use onOptionsItemSelected on my activity to work after the click like that :

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.item1 :
        // here, I would like to open a contextual menu
        return true;
    default :
        return super.onOptionsItemSelected(item);
    }
}

So, when user click on item 1 I would like to open a contextual menu. First, I don't know if it's possible to open a contextual menu directly without using hold position on screen like several tutorials on internet show it.

If it's possible how can I open a contextual menu in that way ?

I thought to use registerForContextMenu() and openContextMenu() in the case of my item 1 but which view should I put in parameter ?

If someone has an idea about the way to make that, I would like to know how I must do that.

解决方案

If you really want to have a contextual menu, then I'd use the contextual menu as it is designed in Android by doing a long-press on a given item. People are accustomed to this and apps shouldn't differ in that.

The alternativ is a bit more complex. What you could do is to have your menu loaded in a separate activity which you style as a dialog window (you know with a faded half-transparent background). This can be easily done by applying the Theme.Dialog style to your activity in the manifest.xml file:

<activity android:name=".activities.TagPopupActivity"
            android:label="Tagging" android:theme="@android:style/Theme.Dialog">
                ...
</activity>

Alternatively you can also directly create a Dialog window as described here. You then get the context (i.e. a list click) by implementing the appropriate click listener in your main list activity and then when the user clicks on the item, you retrieve its id and package it into a bundle that you forward to your "menu"-activity styled as a popup dialog.

这篇关于通过点击菜单选项项打开上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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