Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

android - Get Selected Text from TextView

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
        }

    });

Results

Example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...