在TextView中选择文本时如何覆盖“复制”,“共享”和“全选”选项

时间:2018-10-11 16:15:05

标签: android android-layout textview android-menu

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

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

这是我的看法:

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

在Java代码中,我有:

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

1 个答案:

答案 0 :(得分:2)

您可以使用TextView.setCustomSelectionActionModeCallback()来完成此操作。

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

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

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中。

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

enter image description here enter image description here

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

enter image description here

请注意,无论如何,您都必须return true中的onCreateActionMode()(返回false导致菜单不显示),但是如果满足以下条件,则您只需要return true中的onPrepareActionMode()您实际上已经修改了菜单。

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

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

相关问题