类android.text.Selection源码实例Demo

下面列出了怎么用android.text.Selection的API类实例代码及写法,或者点击链接到github查看源代码。

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    if (!mode.isUiFocusable()) {
        // If the action mode we're running in is not focusable the user
        // will not be able to type into the find on page field. This
        // should only come up when we're running in a dialog which is
        // already less than ideal; disable the option for now.
        return false;
    }

    mode.setCustomView(mCustomView);
    mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_find,
            menu);
    mActionMode = mode;
    Editable edit = mEditText.getText();
    Selection.setSelection(edit, edit.length());
    mMatches.setVisibility(View.GONE);
    mMatchesFound = false;
    mMatches.setText("0");
    mEditText.requestFocus();
    return true;
}
 
@Override
protected boolean pageDown(TextView widget, Spannable buffer) {
    final Layout layout = widget.getLayout();
    final boolean selecting = isSelecting(buffer);
    final int targetY = getCurrentLineTop(buffer, layout) + getPageHeight(widget);
    boolean handled = false;
    for (;;) {
        final int previousSelectionEnd = Selection.getSelectionEnd(buffer);
        if (selecting) {
            Selection.extendDown(buffer, layout);
        } else {
            Selection.moveDown(buffer, layout);
        }
        if (Selection.getSelectionEnd(buffer) == previousSelectionEnd) {
            break;
        }
        handled = true;
        if (getCurrentLineTop(buffer, layout) >= targetY) {
            break;
        }
    }
    return handled;
}
 
源代码3 项目: CallMeMaybe   文件: PhoneFormatter.java
private void formatPhoneNumberInput(final Editable input) {
  final int selection = Selection.getSelectionStart(buffer);
  boolean selectionPositionRemembered = false;

  asYouTypeFormatter.clear();
  String phoneNumberText = "";

  int offset = 0;
  final int length = input.length();
  while (offset < length) {
    final int codePoint = Character.codePointAt(input, offset);
    if (Character.isDigit(codePoint)) {
      final char digit = CodePoint.toDigitChar(codePoint);
      selectionPositionRemembered = selectionPositionRemembered || offset >= selection;
      phoneNumberText = asYouTypeFormatter.inputDigit(digit, !selectionPositionRemembered);
    }
    offset += Character.charCount(codePoint);
  }

  input.replace(0, input.length(), phoneNumberText);
  if (selection != -1) {
    Selection.setSelection(input,
        selectionPositionRemembered ? asYouTypeFormatter.getRememberedPosition() : phoneNumberText.length());
  }
}
 
源代码4 项目: WifiChat   文件: EmoteInputView.java
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    String text = null;
    text = BaseApplication.mEmoticons_Zem.get(arg2);
    if (mEEtView != null && !TextUtils.isEmpty(text)) {
        int start = mEEtView.getSelectionStart();
        CharSequence content = mEEtView.getText().insert(start, text);
        mEEtView.setText(content);
        // 定位光标位置
        CharSequence info = mEEtView.getText();
        if (info instanceof Spannable) {
            Spannable spanText = (Spannable) info;
            Selection.setSelection(spanText, start + text.length());
        }

    }
}
 
源代码5 项目: SimplifySpan   文件: CustomLinkMovementMethod.java
@Override
public boolean onTouchEvent(TextView textView, Spannable spannable, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        mCustomClickableSpan = getPressedSpan(textView, spannable, event);
        if (mCustomClickableSpan != null) {
            mCustomClickableSpan.setPressed(true);
            Selection.setSelection(spannable, spannable.getSpanStart(mCustomClickableSpan), spannable.getSpanEnd(mCustomClickableSpan));
        }
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        CustomClickableSpan touchedSpan = getPressedSpan(textView, spannable, event);
        if (mCustomClickableSpan != null && touchedSpan != mCustomClickableSpan) {
            mCustomClickableSpan.setPressed(false);
            mCustomClickableSpan = null;
            Selection.removeSelection(spannable);
        }
    } else {
        if (mCustomClickableSpan != null) {
            mCustomClickableSpan.setPressed(false);
            super.onTouchEvent(textView, spannable, event);
        }
        mCustomClickableSpan = null;
        Selection.removeSelection(spannable);
    }
    return true;
}
 
源代码6 项目: Trebuchet   文件: Folder.java
public void doneEditingFolderName(boolean commit) {
    mFolderName.setHint(sHintText);
    // Convert to a string here to ensure that no other state associated with the text field
    // gets saved.
    String newTitle = mFolderName.getText().toString();
    if (!SettingsProvider.getBoolean(mLauncher,
            SettingsProvider.SETTINGS_UI_HOMESCREEN_HIDE_ICON_LABELS,
            R.bool.preferences_interface_homescreen_hide_icon_labels_default)) {
        mInfo.setTitle(newTitle);
    }
    LauncherModel.updateItemInDatabase(mLauncher, mInfo);

    if (commit) {
        sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
                String.format(getContext().getString(R.string.folder_renamed), newTitle));
    }
    // In order to clear the focus from the text field, we set the focus on ourself. This
    // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
    requestFocus();

    Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
    mIsEditingName = false;
    mLauncher.notifyFolderNameChanged();
}
 
源代码7 项目: Trebuchet   文件: AllAppsContainerView.java
public AllAppsContainerView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    Resources res = context.getResources();

    mLauncher = (Launcher) context;
    mApps = new AlphabeticalAppsList(context);
    mAdapter = new AllAppsGridAdapter(mLauncher, mApps, this, mLauncher,
            this);
    mApps.setAdapter(mAdapter);
    mLayoutManager = mAdapter.getLayoutManager();
    mItemDecoration = mAdapter.getItemDecoration();
    mRecyclerViewTopBottomPadding =
            res.getDimensionPixelSize(R.dimen.all_apps_list_top_bottom_padding);

    mSearchQueryBuilder = new SpannableStringBuilder();
    Selection.setSelection(mSearchQueryBuilder, 0);
}
 
源代码8 项目: ShaderEditor   文件: UndoRedo.java
public void redo() {
	EditItem edit = editHistory.getNext();
	if (edit == null) {
		return;
	}

	Editable text = textView.getEditableText();
	int start = edit.start;
	int end = start + (edit.before != null ? edit.before.length() : 0);

	isUndoOrRedo = true;
	text.replace(start, end, edit.after);
	isUndoOrRedo = false;

	removeUnderlineSpans(text);

	Selection.setSelection(text, edit.after == null ? start
			: (start + edit.after.length()));
}
 
源代码9 项目: tysq-android   文件: ArticleReportFragment.java
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    String content = etDescription.getText().toString().trim();

    //判断是否显示按钮为可点击
    if (content.length() > 0 && adapter.getReportType() != null){
        btnReport.setBackgroundResource(R.drawable.selector_btn_blue_bg);
        btnReport.setClickable(true);
    }else{
        btnReport.setBackgroundColor(getResources().getColor(R.color.shadow_black_color));
        btnReport.setClickable(false);
    }
    if (content.length() > 50){
        String newStr = content.substring(0,50);
        etDescription.setText(newStr);
        Selection.setSelection(etDescription.getText(), 50);
        ToastUtils.show(getString(R.string.resume_change_num_max));
        content = newStr;
    }
    int num = 50 - content.length();
    tvNum.setText(""+ num);
}
 
private void getInfo(Bundle arguments) {

        G.onChannelCheckUsername = this;

        if (arguments != null) {
            roomId = arguments.getLong("ROOMID");
            inviteLink = "https://" + arguments.getString("INVITE_LINK");
            token = arguments.getString("TOKEN");
        }

        edtSetLink.set(Config.IGAP_LINK_PREFIX);
        wordtoSpan = new SpannableString(edtSetLink.get());
        Selection.setSelection(wordtoSpan, edtSetLink.get().length());

        setInviteLink();
    }
 
源代码11 项目: Pix-Art-Messenger   文件: ListSelectionManager.java
public void onBeforeNotifyDataSetChanged() {
    if (SUPPORTED) {
        HANDLER.removeMessages(MESSAGE_SEND_RESET);
        HANDLER.removeMessages(MESSAGE_RESET);
        HANDLER.removeMessages(MESSAGE_START_SELECTION);
        if (selectionActionMode != null) {
            final CharSequence text = selectionTextView.getText();
            futureSelectionIdentifier = selectionIdentifier;
            futureSelectionStart = Selection.getSelectionStart(text);
            futureSelectionEnd = Selection.getSelectionEnd(text);
            selectionActionMode.finish();
            selectionActionMode = null;
            selectionIdentifier = null;
            selectionTextView = null;
        }
    }
}
 
源代码12 项目: ShaderEditor   文件: UndoRedo.java
public void undo() {
	EditItem edit = editHistory.getPrevious();
	if (edit == null) {
		return;
	}

	Editable text = textView.getEditableText();
	int start = edit.start;
	int end = start + (edit.after != null ? edit.after.length() : 0);

	isUndoOrRedo = true;
	text.replace(start, end, edit.before);
	isUndoOrRedo = false;

	removeUnderlineSpans(text);

	Selection.setSelection(text, edit.before == null ? start
			: (start + edit.before.length()));
}
 
源代码13 项目: canarinho   文件: ValorMonetarioWatcher.java
private void atualizaTexto(Editable editable, String valor) {
    mudancaInterna = true;

    final InputFilter[] oldFilters = editable.getFilters();

    editable.setFilters(new InputFilter[] {});
    editable.replace(0, editable.length(), valor);

    editable.setFilters(oldFilters);

    if (valor.equals(editable.toString())) {
        // TODO: estudar implantar a manutenção da posição do cursor
        Selection.setSelection(editable, valor.length());
    }

    mudancaInterna = false;
}
 
源代码14 项目: LinkTextView   文件: LinkTextView.java
@Override
public boolean onTouchEvent(TextView textView, Spannable spannable, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mPressedSpan = getPressedSpan(textView, spannable, event);
            if (mPressedSpan != null) {
                mPressedSpan.setPressed(true);
                Selection.setSelection(spannable, spannable.getSpanStart(mPressedSpan),
                        spannable.getSpanEnd(mPressedSpan));
            }
            break;

        case MotionEvent.ACTION_MOVE:
            TouchableSpan touchedSpan = getPressedSpan(textView, spannable, event);
            if (mPressedSpan != null && touchedSpan != mPressedSpan) {
                mPressedSpan.setPressed(false);
                mPressedSpan = null;
                Selection.removeSelection(spannable);
            }
            break;

        default:
            if (mPressedSpan != null) {
                mPressedSpan.setPressed(false);
                super.onTouchEvent(textView, spannable, event);
            }
            mPressedSpan = null;
            Selection.removeSelection(spannable);
            break;
    }

    return true;
}
 
源代码15 项目: Overchan-Android   文件: FixedLinkMovementMethod.java
@Override
public void initialize(TextView widget, final Spannable text) {
    widget.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            skipNextClick = true;
            Selection.removeSelection(text);
            return false;
        }
    });
    super.initialize(widget, text);
}
 
源代码16 项目: android_9.0.0_r45   文件: FindActionModeCallback.java
/**
 * Place text in the text field so it can be searched for.  Need to press
 * the find next or find previous button to find all of the matches.
 */
public void setText(String text) {
    mEditText.setText(text);
    Spannable span = (Spannable) mEditText.getText();
    int length = span.length();
    // Ideally, we would like to set the selection to the whole field,
    // but this brings up the Text selection CAB, which dismisses this
    // one.
    Selection.setSelection(span, length, length);
    // Necessary each time we set the text, so that this will watch
    // changes to it.
    span.setSpan(this, 0, length, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    mMatchesFound = false;
}
 
源代码17 项目: android_9.0.0_r45   文件: BaseInputConnection.java
/**
 * Return the target of edit operations.  The default implementation
 * returns its own fake editable that is just used for composing text;
 * subclasses that are real text editors should override this and
 * supply their own.
 */
public Editable getEditable() {
    if (mEditable == null) {
        mEditable = Editable.Factory.getInstance().newEditable("");
        Selection.setSelection(mEditable, 0);
    }
    return mEditable;
}
 
源代码18 项目: delion   文件: FindToolbar.java
@Override
public boolean onTextContextMenuItem(int id) {
    if (id == android.R.id.paste) {
        ClipboardManager clipboard = (ClipboardManager) getContext()
                .getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = clipboard.getPrimaryClip();
        if (clipData != null) {
            // Convert the clip data to a simple string
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < clipData.getItemCount(); i++) {
                builder.append(clipData.getItemAt(i).coerceToText(getContext()));
            }

            // Identify how much of the original text should be replaced
            int min = 0;
            int max = getText().length();

            if (isFocused()) {
                final int selStart = getSelectionStart();
                final int selEnd = getSelectionEnd();

                min = Math.max(0, Math.min(selStart, selEnd));
                max = Math.max(0, Math.max(selStart, selEnd));
            }

            Selection.setSelection(getText(), max);
            getText().replace(min, max, builder.toString());
            return true;
        }
    }
    return super.onTextContextMenuItem(id);
}
 
源代码19 项目: android_9.0.0_r45   文件: BaseInputConnection.java
/**
 * The default implementation returns the given amount of text from the
 * current cursor position in the buffer.
 */
public CharSequence getTextBeforeCursor(int length, int flags) {
    final Editable content = getEditable();
    if (content == null) return null;

    int a = Selection.getSelectionStart(content);
    int b = Selection.getSelectionEnd(content);

    if (a > b) {
        int tmp = a;
        a = b;
        b = tmp;
    }

    if (a <= 0) {
        return "";
    }

    if (length > a) {
        length = a;
    }

    if ((flags&GET_TEXT_WITH_STYLES) != 0) {
        return content.subSequence(a - length, a);
    }
    return TextUtils.substring(content, a - length, a);
}
 
源代码20 项目: android_9.0.0_r45   文件: BaseInputConnection.java
/**
 * The default implementation returns the given amount of text from the
 * current cursor position in the buffer.
 */
public CharSequence getTextAfterCursor(int length, int flags) {
    final Editable content = getEditable();
    if (content == null) return null;

    int a = Selection.getSelectionStart(content);
    int b = Selection.getSelectionEnd(content);

    if (a > b) {
        int tmp = a;
        a = b;
        b = tmp;
    }

    // Guard against the case where the cursor has not been positioned yet.
    if (b < 0) {
        b = 0;
    }

    if (b + length > content.length()) {
        length = content.length() - b;
    }


    if ((flags&GET_TEXT_WITH_STYLES) != 0) {
        return content.subSequence(b, b + length);
    }
    return TextUtils.substring(content, b, b + length);
}
 
源代码21 项目: CSipSimple   文件: DialerFragment.java
/**
 * Set the value of the text field and put caret at the end
 * 
 * @param value the new text to see in the text field
 */
public void setTextFieldValue(CharSequence value) {
    if(digits == null) {
        initText = value.toString();
        return;
    }
    digits.setText(value);
    // make sure we keep the caret at the end of the text view
    Editable spannable = digits.getText();
    Selection.setSelection(spannable, spannable.length());
}
 
源代码22 项目: android_9.0.0_r45   文件: CharacterPickerDialog.java
private void replaceCharacterAndClose(CharSequence replace) {
    int selEnd = Selection.getSelectionEnd(mText);
    if (mInsert || selEnd == 0) {
        mText.insert(selEnd, replace);
    } else {
        mText.replace(selEnd - 1, selEnd, replace);
    }

    dismiss();
}
 
源代码23 项目: android_9.0.0_r45   文件: LinkMovementMethod.java
@Override
public void onTakeFocus(TextView view, Spannable text, int dir) {
    Selection.removeSelection(text);

    if ((dir & View.FOCUS_BACKWARD) != 0) {
        text.setSpan(FROM_BELOW, 0, 0, Spannable.SPAN_POINT_POINT);
    } else {
        text.removeSpan(FROM_BELOW);
    }
}
 
源代码24 项目: android_9.0.0_r45   文件: ArrowKeyMovementMethod.java
@Override
protected boolean left(TextView widget, Spannable buffer) {
    final Layout layout = widget.getLayout();
    if (isSelecting(buffer)) {
        return Selection.extendLeft(buffer, layout);
    } else {
        return Selection.moveLeft(buffer, layout);
    }
}
 
源代码25 项目: android_9.0.0_r45   文件: ArrowKeyMovementMethod.java
@Override
protected boolean right(TextView widget, Spannable buffer) {
    final Layout layout = widget.getLayout();
    if (isSelecting(buffer)) {
        return Selection.extendRight(buffer, layout);
    } else {
        return Selection.moveRight(buffer, layout);
    }
}
 
源代码26 项目: android_9.0.0_r45   文件: ArrowKeyMovementMethod.java
@Override
protected boolean up(TextView widget, Spannable buffer) {
    final Layout layout = widget.getLayout();
    if (isSelecting(buffer)) {
        return Selection.extendUp(buffer, layout);
    } else {
        return Selection.moveUp(buffer, layout);
    }
}
 
源代码27 项目: CallMeMaybe   文件: PhoneStringBuilder.java
@Override
public SpannableStringBuilder replace(final int start, final int end, final CharSequence text,
    final int textStart, final int textEnd) {
  phoneFormatter.format(this, start, end, text, textStart, textEnd);
  final CharSequence phoneNumber = phoneFormatter.getFormattedPhone();
  final SpannableStringBuilder result = super.replace(0, length(), phoneNumber, 0, phoneNumber.length());
  final int selection = phoneFormatter.getSelection();
  if (selection != -1 && selection <= result.length()) {
    Selection.setSelection(result, selection);
  }
  return result;
}
 
源代码28 项目: android_9.0.0_r45   文件: ArrowKeyMovementMethod.java
@Override
protected boolean top(TextView widget, Spannable buffer) {
    if (isSelecting(buffer)) {
        Selection.extendSelection(buffer, 0);
    } else {
        Selection.setSelection(buffer, 0);
    }
    return true;
}
 
源代码29 项目: fanfouapp-opensource   文件: WritePage.java
private void insertNames(final Intent intent) {
    final String names = intent.getStringExtra(Constants.EXTRA_TEXT);
    if (AppContext.DEBUG) {
        log("doAddUserNames: " + names);
    }
    if (!StringHelper.isEmpty(names)) {
        final Editable editable = this.mAutoCompleteTextView
                .getEditableText();
        editable.append(names);
        Selection.setSelection(editable, editable.length());
    }

}
 
源代码30 项目: android_9.0.0_r45   文件: ArrowKeyMovementMethod.java
/** {@hide} */
@Override
protected boolean leftWord(TextView widget, Spannable buffer) {
    final int selectionEnd = widget.getSelectionEnd();
    final WordIterator wordIterator = widget.getWordIterator();
    wordIterator.setCharSequence(buffer, selectionEnd, selectionEnd);
    return Selection.moveToPreceding(buffer, wordIterator, isSelecting(buffer));
}
 
 类所在包
 同包方法