从 TextView 中获取选定的文本 [英] Get Selected Text from TextView

查看:10
本文介绍了从 TextView 中获取选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取用户在 TextView 中选择的文本,我不想使用 android:textIsSelectable="true" 来允许我的用户复制/粘贴操作

I'm trying to get the text the user selected in a TextView, i wan't to use the android:textIsSelectable="true" to allow my user copy/paste actions

但是,一旦显示操作栏菜单,我不知道如何获取文本,目标是实现类似 Google 图书的行为:您选择一个单词,它会为您提供定义.

However I don't have a clue as how to get the text once the action bar menu is displayed, the goal is to implement a Google book like behavior : you select a word and it gives you a definition.

推荐答案

我想你要找的是 TextView.setCustomSelectionActionModeCallback.这将允许您在选择文本时创建自己的 ActionMode.Callback.然后,当您选择 MenuItem 时,您可以使用 TextView.getSelectionStartTextView.getSelectionEnd 检索所选文本.这是一个简单的例子:

I think what you're looking for is TextView.setCustomSelectionActionModeCallback. This will allow you to create your own ActionMode.Callback for when the text is selected. Then you can use TextView.getSelectionStart and TextView.getSelectionEnd to retrieve the selected text when your MenuItem is selected. Here's a quick example:

    mTextView.setCustomSelectionActionModeCallback(new Callback() {

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // Remove the "select all" option
            menu.removeItem(android.R.id.selectAll);
            // Remove the "cut" option
            menu.removeItem(android.R.id.cut);
            // Remove the "copy all" option
            menu.removeItem(android.R.id.copy);
            return true;
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Called when action mode is first created. The menu supplied
            // will be used to generate action buttons for the action mode

            // Here is an example MenuItem
            menu.add(0, DEFINITION, 0, "Definition").setIcon(R.drawable.ic_action_book);
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // Called when an action mode is about to be exited and
            // destroyed
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case DEFINITION:
                    int min = 0;
                    int max = mTextView.getText().length();
                    if (mTextView.isFocused()) {
                        final int selStart = mTextView.getSelectionStart();
                        final int selEnd = mTextView.getSelectionEnd();

                        min = Math.max(0, Math.min(selStart, selEnd));
                        max = Math.max(0, Math.max(selStart, selEnd));
                    }
                    // Perform your definition lookup with the selected text
                    final CharSequence selectedText = mTextView.getText().subSequence(min, max);
                    // Finish and close the ActionMode
                    mode.finish();
                    return true;
                default:
                    break;
            }
            return false;
        }

    });

结果

这篇关于从 TextView 中获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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