onCreateContextMenu()的EditText上不上真实的设备工作 [英] onCreateContextMenu() for EditText doesn't work on real device

查看:165
本文介绍了onCreateContextMenu()的EditText上不上真实的设备工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想测试我的实际设备(HTC Desire Z的与Android 2.2)的应用程序。而且发现,我的上下文菜单并不对的EditText 工作的。否则,上下文菜单的工作原理:在的ListView 的ImageView 等。在仿真器一切正常......

Just tried to test my app on real device (HTC Desire Z with Android 2.2). And found that my context menus doesn't work at all on EditTexts. Otherwise context menus works: in ListView, ImageView and so on. On emulator everything works fine...

当我敲击的EditText它显示了类似的变焦范围,然后显示异常的(不是标准的Andr​​oid一样)上下文内容如下菜单:选择文本,全选。它不显示我的菜单。 以下是截图:

When I tap on EditText it shows something like zoom frame and then shows unusual (not standard Android alike) context menu which reads: "select text", "select all". It doesn't show my menus. Here are screenshots:

  1. <一个href="https://docs.google.com/leaf?id=0B64TN1V7krz9NWQ5MzRiNjAtMWMxYS00MmU4LWJmNzYtNzRkODE3MTVhOTc0&hl=en">Before点击
  2. <一个href="https://docs.google.com/leaf?id=0B64TN1V7krz9ZThiNjA0NjctN2Y5MC00NTljLWFhMjQtYThmZmI5ZmViYWJi&hl=en">During点击
  3. <一个href="https://docs.google.com/leaf?id=0B64TN1V7krz9MTU3MmZkODItMTdkZi00ZTJkLWI1MzQtM2Q3NjY2NTU2NDM4&hl=en">After挖掘(只是普通的选择文本,全选,粘贴)
  1. Before tap
  2. During tap
  3. After tap (just ordinary select text, select all, paste)

但没有我的菜单像模拟器 - <一个href="https://docs.google.com/leaf?id=0B64TN1V7krz9NGVjZWQ3ZmMtNWJhMi00NTgzLWEyMjQtNWExOGJlZTMzZmIz&hl=en">look这里

but no my menu like in emulator - look here

下面的源$ C ​​$我的活动C:

Here's source code of my activity:

public class MyActivity extends Activity
{
    private static final String TAG=MyActivity.class.getName();

    EditText editText;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editText=(EditText )findViewById(R.id.editText);
        this.registerForContextMenu(editText);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo)
    {
        Log.v(TAG, "Creating context menu for view="+view);
        menu.add(Menu.NONE, Menu.FIRST+1, Menu.NONE, "Test menu");
        super.onCreateContextMenu(menu, view, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        Log.v(TAG, "Context item selected as="+item.toString());
        return super.onContextItemSelected(item);
    }
}

我已经彻底的调试/记录的一切都在我的code,但Activity.onCreateContextMenu()甚至没有所谓的(尽管它记录在适当的方式)。

I have thoroughly debugged/logged everything around my code, but still Activity.onCreateContextMenu() not even called (though it's registered in proper way).

请帮忙 - 是什么可以吗?是否涉及到HTC的特性呢?

Please help - what it can be? Does it related to HTC peculiarities?

推荐答案

是的,我认为你所看到的有一个HTC的菜单。

Yes, I think what you are seeing there is an HTC menu.

您所看到的图形编辑上下文菜单,我相信,新的更近期的欲望模型(Desire HD的与Desire Z)。我看到(你的code)关于我的原始欲望与HeaderTitle设置为编辑文字和一个列表,如全选弹出基于文本的菜单,复制,粘贴等。但是,我也看到一个条目说:测试菜​​单。

The graphical edit context menu you are seeing is, I believe, new to more recent Desire models (Desire HD and Desire Z). What I see (with your code) on my original Desire is a popup text-based menu with the HeaderTitle set as 'Edit text' and a list such as 'Select all', 'Copy', 'Paste' etc. BUT, I do also see an entry saying 'Test menu'.

我突然想起,当onCreateContextMenu()被调用的东西像ListView和ImageView的,在这一号召通过菜单对象不是pre填充默认。在的情况下关闭一个EditText,但是,它被设计成与剪贴板相互作用,因此该系统提供基于所述的EditText的内容的状态(例如,如果选择了文本,提供一个复制一个$ P $对 - 填充菜单'选项;如果有文本在剪贴板上提供了一个粘贴选项; ...等)

It occurs to me that when onCreateContextMenu() is called for things like ListView and ImageView, the 'menu' object passed in to that call isn't pre-populated by default. In the case off an EditText, however, it is designed to interact with the Clipboard and as such the system is providing a pre-populated menu based on the EditText's content state (e.g., if text is selected, provide a 'Copy' option; if there's text on the Clipboard provide a 'Paste' option; ...and so on).

我是能够得到一个上下文菜单中没有编辑选项,通过修改您的code清除标题和内容...

I was able to get a Context menu without the 'edit' options by clearing the header and contents by modifying your code...

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo)
{
    Log.v(TAG, "Creating context menu for view="+view);

    // Clear current contents
    menu.clearHeader();
    menu.clear();

    menu.setHeaderTitle("TEST MENU");
    menu.add(Menu.NONE, Menu.FIRST+1, Menu.NONE, "Test menu");
    super.onCreateContextMenu(menu, view, menuInfo);
}

你得到一个图形化的菜单(可能是从文本菜单派生)的事实表明,上述技术将不能工作,所以解决它的唯一办法是实例化自己的ContextMenu对象,而不是使用一个传递到onCreateContextMenu( )。

The fact you're getting a graphical menu (possibly derived from ContextMenu) suggests the above technique won't work so the only way around it would be to instantiate your own ContextMenu object rather than use the one passed in to onCreateContextMenu().

我已经彻底的调试/记录的一切都在我的code,但Activity.onCreateContextMenu()甚至不叫

I have thoroughly debugged/logged everything around my code, but still Activity.onCreateContextMenu() not even called

这似乎很奇怪 - 显然,这是被称为对我来说,我已经能够玩与被传递给它的文本菜单

This seems very odd - obviously it's being called for me as I've been able to play around with the ContextMenu that is being passed to it.

修改1:重新思考这一点,你提到你水龙头的EditText上 - 这就是你实际做的(短触摸下来再用手指上)

EDIT 1: Re-thinking this, you mention that you 'tap' the EditText - is that what you are actually doing (a short touch down then finger up)?

要得到我的ContextMenu我必须使用'长'preSS /点击(触摸并按住1秒左右)。当我只需点击/摸摸我的EditText出现的软键盘,如果键盘已经可见,将光标移动只是在盒子的EditText不同的位置。

To get my ContextMenu I have to use a 'long' press/click (touch and hold for about 1 second). When I simply tap/touch my EditText the 'soft' keyboard appears or, if the keyboard is already visible, the cursor is simply moved to a different position in the EditText box.

显然,Desire Z的有物理键盘,并且可能会导致稍有不同的行为(以及具有不同版本的Sense UI,以我的欲望Desire Z的)。

Obviously the Desire Z has a physical keyboard and that may cause slightly different behaviour (as well as the Desire Z having a different version of the Sense UI to my Desire).

这onCreateContextMenu()永远不会要求您可以只意味着一件事,事实上,你所看到的不是一个文本菜单和一些其他类型的弹出式UI元素。至少这是我能理解它的唯一合乎逻辑的方式。

The fact that onCreateContextMenu() is never called for you can mean only one thing and that is, what you are seeing is NOT a ContextMenu and is some other type of popup UI element. At least that is the only logical way I can understand it.

可以确认,多头preSS仍不能创建一个文本菜单你或你使用的是长preSS已经全部一起去吗?

Can you confirm that a 'long' press still doesn't create a ContextMenu for you or have you been using a long press all along?

如果您已经尝试了很长preSS然后尝试修改code如下...

If you have tried a long press then try modifying the code as follows...

  1. 实现OnClickListener

  1. Implement OnClickListener

public class MyActivity extends Activity
    implements OnLongClickListener {

  • 设置侦听EDITTEXT中的onCreate ...

  • Set the listener for editText in onCreate...

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editText=(EditText )findViewById(R.id.editText);
        this.registerForContextMenu(editText);
        editText.setOnLongClickListener(this); // <-- ADD THIS
    }
    

  • 添加监听code ...

  • Add the listener code...

    @Override
    public boolean onLongClick(View arg0) {
        android.util.Log.v(TAG, "onLongClick() called");
        if (arg0 == editText)
        {
            android.util.Log.v(TAG, "arg0 == editText");
                Toast.makeText(this, "onLongClick intercepted", 2000).show();
            return true;
        }
        else
        {
            android.util.Log.v(TAG, "arg0 != editText");
            return false;
        }
    }
    

  • 这样,我能够拦截从长远preSS和,由onLongClick()我要说明,我已经消耗事件的系统返回真,并没有关系吨得到传递,使我的创作文本菜单的。

    By doing this, I'm able to intercept the long press and, by returning 'true' from onLongClick() I'm indicating to the system that I've 'consumed' the event and it doesn't get passed on to cause the creation of my ContextMenu.

    如果这不会给你和短抽头的工作仍然会导致出现弹出,然后尝试实现OnClickListener和重写的onClick()来代替。

    If this doesn't work for you and a short tap still causes that popup to appear, then try implementing OnClickListener and overriding onClick() instead.

    演习的目的是,如果你可以拦截任何原因造成的创作,你所看到的弹出窗口,则可以手动创建并显示自己的文本菜单。

    The object of the exercise is that if you can intercept whatever is causing the creation of the popup you're seeing, you can then manually create and show your own ContextMenu.

    这篇关于onCreateContextMenu()的EditText上不上真实的设备工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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