EditText 如何在没有任何操作栏的情况下激活复制/粘贴弹出窗口? [英] EditText how to activate copy/paste popup without any actionbar?

查看:19
本文介绍了EditText 如何在没有任何操作栏的情况下激活复制/粘贴弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 delphi 下,我使用 android 框架来创建编辑.当我将主题设置为 Theme.DeviceDefault.Light.NoActionBar 时,我可以在我的 EditText 中选择一些文本,然后我会弹出一个带有全选/剪切/复制/粘贴/等"的弹出窗口,就像你可以见下图.

I m under delphi and i use the android framework to create an edit. When i set the theme to Theme.DeviceDefault.Light.NoActionBar then i can select some text in my EditText and i have a popup with "select all/cut/copy/paste/etc" like you can see on the picture below.

但是,当我选择 Theme.Material.Light.NoActionBarTheme.Holo.Light.NoActionBar 时,我 无法选择我的 EditText 中的任何文本(我没有左右文本选择句柄)当然我没有任何复制/粘贴弹出窗口

However, when i select Theme.Material.Light.NoActionBar or Theme.Holo.Light.NoActionBar then i can't select any text in my EditText (i have no right or left text selection handles) and off course i don't have any copy/paste popup

他们有什么方法可以在 Theme_Material_Light_NoActionBar 上有这个复制/粘贴弹出窗口吗?

Is their any way to have this copy/paste popup on Theme_Material_Light_NoActionBar ?

Theme.DeviceDefault.Light.NoActionBar

Theme.DeviceDefault.Light.NoActionBar

Theme_Material_Light_NoActionBar

Theme_Material_Light_NoActionBar

Theme.Holo.Light.NoActionBar

Theme.Holo.Light.NoActionBar

注意 1:

当我将屏幕移动到水平位置时,edittext 会占用所有可用空间,然后我可以看到如下图所示的左右文本选择句柄,但我认为这是因为主题交换为 Theme.DeviceDefault.Light.NoActionBar 当我将屏幕移动到水平位置但我不确定:

When i move the screen to the horizontal then the edittext take all the available space, and then i can see my right and left text selection handles like on the picture below, but i think it's because theme swap to Theme.DeviceDefault.Light.NoActionBar when i move the screen to the horizontal but i m not sure :

注意 2:

在我的 editText 上,当我执行 setCustomSelectionActionModeCallback(new Callback() {}) 时,Callback 永远不会被调用 :( 我认为这不正常?editText 中的什么可以禁止回调?

On my editText, when i do setCustomSelectionActionModeCallback(new Callback() {}) then the Callback is never called :( this is not normal i think ? What in the editText can forbid the callback ?

注意 2:

我可以在所有主题中选择文本(但我不能复制它),但除了 Theme.DeviceDefault.Light.NoActionBar 我看不到左右文本选择句柄.

I can select text in all theme (but i can't copy it off course), but except in Theme.DeviceDefault.Light.NoActionBar i can't see the right and left text selection handle.

注意 3:

Theme.DeviceDefault.Light.NoActionBar 仅在三星 Galaxy 等某些手机上显示左右文本选择句柄.在其他一些情况下,它不起作用.

Theme.DeviceDefault.Light.NoActionBar show the right and left text selection handle only on some phones like the samsung galaxy. On some other it's didn't work.

注意 4:

我找到了部分问题的根源!这是因为我通过 WindowManager.addView(view, layout_params) 创建了我的视图,并以这种方式 startactionmodeforChild 返回 null ,这禁止显示操作栏和文本选择句柄.现在如果我在我的编辑文本中做这样的事情:

i found partially the source of the problem! it's because i create my view via the WindowManager.addView(view, layout_params) and in this way startactionmodeforChild return null and this forbid the actionbar and the text selection handles to be show. Now if i do something like this in my edittext :

@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
    Activity host = (Activity) this.getContext();    
    return host.getWindow().getDecorView().startActionMode(callback); 
} 

然后我可以看到左右文本操作句柄(但不是在棉花糖上,它只在棒棒糖上有效,我不知道为什么).我现在的问题是显示了操作栏,但它显示为空 :( 内部没有绘制任何内容(但我可以看到剪切/复制/过去控件在转储视图层次结构中).所以现在我没有寻找方法用弹出菜单替换此操作栏(如图片 Theme.DeviceDefault.Light.NoActionBar 上).知道吗?

then i can see the right and left text action handles (but not on marshmallow, it's work only on lollipop this i don't know why). My problem now is that the actionbar is showed, but it's showed empty :( nothing is draw inside (but i can see that the cut/copy/past control are inside in the dump view hierarchie). So now i m not looking for a way to replace this actionbar by a popup menu instead (like on the picture Theme.DeviceDefault.Light.NoActionBar). Any idea ?

推荐答案

在你的活动中添加关注

ActionMode mActionMode;

你必须创建一个 ActionMondeCallback 接口

and you have to create an ActionMondeCallback interface

class ActionBarCallback implements ActionMode.Callback
    {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

            int id = item.getItemId();
            if(id == R.id.item_delete)
            {
                tv.setText("");
                Toast.makeText(MainActivity.this,"option deleted",Toast.LENGTH_LONG);
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    }

其中 contextual_menu.xml 如下所示,带有必需的图标

where contextual_menu.xml is as follows with required icons

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.letschat"
    >
    <item
        android:id="@+id/item_search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="ifRoom|withText"
        android:title="Delete"
        android:titleCondensed="Delete">
    </item>
    <item
        android:id="@+id/item_delete"
        android:icon="@android:drawable/ic_menu_delete"
        app:showAsAction="ifRoom|withText"
        android:title="Delete"
        android:titleCondensed="Delete">
    </item>
    <item
        android:id="@+id/item_share"
        android:icon="@android:drawable/ic_menu_share"
        app:showAsAction="ifRoom|withText"
        android:title="Delete"
        android:titleCondensed="Delete">
    </item>
</menu>

现在启用您的 Contextual ActionBar(CAB) 如下所示,例如这里是在长按 textview 时启用

Now Enable your Contextual ActionBar(CAB) As follows as for example here am are enabling on long click of a textview

yourtextView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mActionMode = MainActivity.this.startActionMode(new ActionBarCallback());
                return true;
            }
        });

那么你必须在 CAB 上的每个动作事件上编写你自己的点击动作.

then you have to write your own action on click on each action event on CAB.

~赏金猎人更多详情

这篇关于EditText 如何在没有任何操作栏的情况下激活复制/粘贴弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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