【问题标题】:EditText not showing default ContextMenu after long press长按后 EditText 不显示默认的 ContextMenu
【发布时间】:2014-12-27 07:28:32
【问题描述】:

长按后,我的 EditText 不显示默认的 ContextMenu(复制、粘贴、选择、全选)。我必须创建自己的 ContextMenu 吗?

下面是来自一个函数的 sn-ps,该函数被调用以创建此 EditText 所在的弹出菜单。

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    popupView = layoutInflater.inflate(R.layout.add_recipe_pop,null);
    final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    EditText recipe_url = (EditText)popupView.findViewById(R.id.recipe_url_text);
    recipe_url.setLongClickable(true);
    registerForContextMenu(recipe_url);
    popupWindow.setFocusable(true);
    popupWindow.update();
    popupWindow.showAtLocation(v,Gravity.CENTER,0,0);

这是 add_recipe_pop XML 的一部分,而 EditText 只是在一个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#E6E6E6"
    android:orientation="vertical" >

<EditText
     android:id="@+id/recipe_url_text"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:hint="@string/add_recipe_hint"
     android:inputType="textUri"
   />

我尝试过使用 EditText 可聚焦和 setTextSelectable 属性,但如果我这样做,键盘不会出现。感谢您的帮助! :)

【问题讨论】:

  • 该功能称为剪贴板,请查看此链接。 stackoverflow.com/questions/25580288/…
  • 使用 setfocusableintouchmode.. 不,您不必创建自己的上下文菜单,是设备默认设置.. 并删除 recipe_url.setLongClickable(true); registerForContextMenu(recipe_url);
  • @Elltz 我尝试使用recipe_url.setFocusableInTouchMode(true);但上下文菜单仍然没有出现:/
  • @balajikoduri 我试过这样做,它有效,但它只允许我粘贴或复制,但我希望能够执行所有功能(复制、粘贴、选择、全选)

标签: android android-edittext contextmenu


【解决方案1】:

这是旧的,但对于任何看到这个的人,我发现没有办法解决这个问题。

由于某种原因,弹出窗口中的任何编辑文本都可以使用,但不能与复制/粘贴菜单一起使用。

实际上你可以设置任何文本可选,但它不会起作用。

解决此问题的最简单方法就是不使用弹出菜单。如果您更改代码以将内容视图设置为弹出菜单布局,它将起作用。

为什么它在弹出菜单中不起作用我不知道,但不使用菜单是有效的。

【讨论】:

    【解决方案2】:
    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    popupView = layoutInflater.inflate(R.layout.add_recipe_pop,null);
    final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    EditText recipe_url = (EditText)popupView.findViewById(R.id.recipe_url_text);
    popupWindow.update();
    popupWindow.showAtLocation(v,Gravity.CENTER,0,0);
    

    xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#E6E6E6"
    android:focusable="true"   // toggle this when testing
    android:orientation="vertical" >
    
    <EditText
     android:id="@+id/recipe_url_text"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:hint="@string/add_recipe_hint"
     android:focusable="true"
     android:focusableInTouchMode="true"
     android:inputType="textUri"
    />
    

    这应该有效..让我知道它是否有效,所以我给出我的理由..

    【讨论】:

    • 它不起作用:/ 我尝试在线性布局中切换可聚焦,但没有任何效果,如果我取出 popupWindow.setFocusable(true),那么我看不到键盘和上下文菜单仍然没有出现。您认为在 popupWindow 中为这些按钮设置额外的按钮和点击侦听器会影响它吗?因为我有 2 个带有监听器的按钮...
    • 是的,我的想法是,让焦点远离视图组,所以当您在软输入键盘弹出之前单击编辑文本时,我也不认为听众会阻止这种情况..@贾斯汀坦
    • 哦..不..我试试一个可行的样本并将其发送给你这里,所以,是的@JustinTan
    • 嘿@Elltz,我做了更多的挖掘,我认为这只是Android中的一个错误。感谢您的所有帮助,但我想我会以其他方式实现它(可能是一个对话框)。
    【解决方案3】:

    这个问题的关键在于setText()时,必须在Window中添加EditText才能支持选择。

    这是关键源代码:Editor.prepareCursorControllers()

    void prepareCursorControllers() {
        boolean windowSupportsHandles = false;
    
        ViewGroup.LayoutParams params = mTextView.getRootView().getLayoutParams();
        // If mTextView is not added to the window, params will be normal LayoutParams(FrameLayout.LayoutParams/LinearLayout.LayoutParams ect.)
        if (params instanceof WindowManager.LayoutParams) {
            WindowManager.LayoutParams windowParams = (WindowManager.LayoutParams) params;
            windowSupportsHandles = windowParams.type < WindowManager.LayoutParams.FIRST_SUB_WINDOW
                    || windowParams.type > WindowManager.LayoutParams.LAST_SUB_WINDOW;
        }
    
        boolean enabled = windowSupportsHandles && mTextView.getLayout() != null;
        mInsertionControllerEnabled = enabled && isCursorVisible();
        mSelectionControllerEnabled = enabled && mTextView.textCanBeSelected();
        /// ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      相关资源
      最近更新 更多