android 实现选中一个单词官方源码

来源:互联网 发布:淘宝购物最便宜的软件 编辑:程序博客网 时间:2024/06/05 12:01


安卓实现单击选中一个单词


android 4.2

Editor.java

    private boolean selectCurrentWord() {        if (!canSelectText()) {            return false;        }        if (hasPasswordTransformationMethod()) {            // Always select all on a password field.            // Cut/copy menu entries are not available for passwords, but being able to select all            // is however useful to delete or paste to replace the entire content.            return mTextView.selectAllText();        }        int inputType = mTextView.getInputType();        int klass = inputType & InputType.TYPE_MASK_CLASS;        int variation = inputType & InputType.TYPE_MASK_VARIATION;        // Specific text field types: select the entire text for these        if (klass == InputType.TYPE_CLASS_NUMBER ||                klass == InputType.TYPE_CLASS_PHONE ||                klass == InputType.TYPE_CLASS_DATETIME ||                variation == InputType.TYPE_TEXT_VARIATION_URI ||                variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS ||                variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS ||                variation == InputType.TYPE_TEXT_VARIATION_FILTER) {            return mTextView.selectAllText();        }        long lastTouchOffsets = getLastTouchOffsets();        final int minOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets);        final int maxOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets);        // Safety check in case standard touch event handling has been bypassed        if (minOffset < 0 || minOffset >= mTextView.getText().length()) return false;        if (maxOffset < 0 || maxOffset >= mTextView.getText().length()) return false;        int selectionStart, selectionEnd;        // If a URLSpan (web address, email, phone...) is found at that position, select it.        URLSpan[] urlSpans = ((Spanned) mTextView.getText()).                getSpans(minOffset, maxOffset, URLSpan.class);        if (urlSpans.length >= 1) {            URLSpan urlSpan = urlSpans[0];            selectionStart = ((Spanned) mTextView.getText()).getSpanStart(urlSpan);            selectionEnd = ((Spanned) mTextView.getText()).getSpanEnd(urlSpan);        } else {            final WordIterator wordIterator = getWordIterator();            wordIterator.setCharSequence(mTextView.getText(), minOffset, maxOffset);            selectionStart = wordIterator.getBeginning(minOffset);            selectionEnd = wordIterator.getEnd(maxOffset);            if (selectionStart == BreakIterator.DONE || selectionEnd == BreakIterator.DONE ||                    selectionStart == selectionEnd) {                // Possible when the word iterator does not properly handle the text's language                long range = getCharRange(minOffset);                selectionStart = TextUtils.unpackRangeStartFromLong(range);                selectionEnd = TextUtils.unpackRangeEndFromLong(range);            }        }        Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd);        return selectionEnd > selectionStart;    }
android 2.3.3

TextView.java

 private void selectCurrentWord() {        // In case selection mode is started after an orientation change or after a select all,        // use the current selection instead of creating one        if (hasSelection()) {            return;        }        int minOffset, maxOffset;        if (mContextMenuTriggeredByKey) {            minOffset = getSelectionStart();            maxOffset = getSelectionEnd();        } else {            // hasSelectionController is true since we canSelectText.            SelectionModifierCursorController selectionModifierCursorController =                (SelectionModifierCursorController) getSelectionController();            minOffset = selectionModifierCursorController.getMinTouchOffset();            maxOffset = selectionModifierCursorController.getMaxTouchOffset();        }        int selectionStart, selectionEnd;        long wordLimits = getWordLimitsAt(minOffset);        if (wordLimits >= 0) {            selectionStart = extractRangeStartFromLong(wordLimits);        } else {            selectionStart = Math.max(minOffset - 5, 0);        }        wordLimits = getWordLimitsAt(maxOffset);        if (wordLimits >= 0) {            selectionEnd = extractRangeEndFromLong(wordLimits);        } else {            selectionEnd = Math.min(maxOffset + 5, mText.length());        }        Selection.setSelection((Spannable) mText, selectionStart, selectionEnd);    }