如何覆盖“复制",“共享"和“全选"在TextView中选择文本时的选项 [英] How to override "Copy", "Share" and "Select All" options while selecting a text in a TextView

查看:295
本文介绍了如何覆盖“复制",“共享"和“全选"在TextView中选择文本时的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextView,用户可以在其中选择文本.默认情况下,出现以下选项:复制",共享"和全选". 我需要使用自定义选项覆盖它们.但我找不到该怎么做.我浏览了文档,并这篇不错的文章但不乏.本文介绍了当用户按下三点按钮时如何扩展菜单,这不是我所需要的.

I have a TextView where a user is able to select a text. By default the following options appear: "Copy", "Share" and "Select All". I need to override them with custom options. But I can't find how to do that. I went through the documentation and this nice article but no lack. The article explains how to extend the menu when a user presses three-dots-button which is not what I need.

问题:如何在文本部分菜单中覆盖默认的复制",共享"和全选"选项?

Question: How can I override default "Copy", "Share" and "Select All" options in text section menu?

这是我的看法:

<TextView
    android:id="@+id/transcript"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical" />

在Java代码中,我有:

And in java code I have:

transcript.setTextIsSelectable(true);
transcript.setFocusable(true);
transcript.setFocusableInTouchMode(true);

推荐答案

您可以使用TextView.setCustomSelectionActionModeCallback()来做到这一点.

You can use TextView.setCustomSelectionActionModeCallback() to do this.

文档: https://developer.android.com/reference/android/widget/TextView.html#setCustomSelectionActionModeCallback(android.view.ActionMode.Callback)

我整理了一个非常简单的应用程序来演示如何使用此功能.

I put together a very simple app to demonstrate how to use this feature.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView text = (TextView) findViewById(R.id.text);

        CustomActionModeCallback callback = new CustomActionModeCallback(this);
        text.setCustomSelectionActionModeCallback(callback);
    }
}

activity_main.xml

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        android:text="@string/lorem_ipsum"
        android:textIsSelectable="true"/>

</FrameLayout>

CustomActionModeCallback.java

public class CustomActionModeCallback implements ActionMode.Callback {

    private final Context context;

    public CustomActionModeCallback(Context context) {
        this.context = context;
    }

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

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

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        if (item.getItemId() == R.id.custom_one) {
            Toast.makeText(context, "One!", Toast.LENGTH_SHORT).show();
            mode.finish();
            return true;
        }
        else if (item.getItemId() == R.id.custom_two) {
            Toast.makeText(context, "Two!", Toast.LENGTH_SHORT).show();
            mode.finish();
            return true;
        }
        else if (item.getItemId() == R.id.custom_three) {
            Toast.makeText(context, "Three!", Toast.LENGTH_SHORT).show();
            mode.finish();
            return true;
        }

        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {

    }
}

menu_custom.xml

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/custom_one"
        android:title="One"
        app:showAsAction="never"/>

    <item
        android:id="@+id/custom_two"
        android:title="Two"
        app:showAsAction="never"/>

    <item
        android:id="@+id/custom_three"
        android:title="Three"
        app:showAsAction="never"/>

</menu>

MainActivity或xml文件中没有什么可评论的.所有的魔术都发生在CustomActionModeCallback.

Nothing much to comment on in MainActivity or either xml file. All the magic happens in CustomActionModeCallback.

onCreateActionMode()onPrepareActionMode()均可用于将自定义菜单项添加到菜单.如果使用onCreateActionMode(),系统将在溢出菜单中添加一些其他选项,如下所示:

Both onCreateActionMode() and onPrepareActionMode() can be used to add your custom menu items to the menu. If you use onCreateActionMode(), the system will add some extra options into an overflow menu, like this:

如果使用onPrepareActionMode(),则不会添加多余的项目.

If you use onPrepareActionMode(), the extra items won't be added.

请注意,无论如何,您都必须从onCreateActionMode()中选择return true(返回false将导致不显示菜单),但是如果您实际上已经修改了菜单,则只需从onPrepareActionMode()中选择return true.

Note that you must return true from onCreateActionMode() no matter what (returning false causes the menu to not be displayed), but you only have to return true from onPrepareActionMode() if you've actually modified the menu.

您可以在onActionItemClicked()中处理用户对自定义项目的点击.在我的示例中,我只显示一个Toast,然后关闭上下文菜单(使用ActionMode.finish()).在这种方法中,您应该仅return true在您自己处理的菜单项上;返回false将允许系统执行默认操作(例如,如果您想为用户提供选择所有文本的选项).

You can handle the user's clicks on your custom items inside onActionItemClicked(). In my example, I simply show a Toast and then close the contextual menu (using ActionMode.finish()). In this method, you should return true only on menu items that you handle yourself; returning false allows the system default action to happen (such as if you want to give the user the option to select all text).

最后,关闭菜单时将调用onDestroyActionMode().也许您对此有一些用途;我没有.

Finally, onDestroyActionMode() is called when the menu is closed. Perhaps you have some use for this; I did not.

这篇关于如何覆盖“复制",“共享"和“全选"在TextView中选择文本时的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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