Java Code Examples for android.widget.EditText#getSelectionStart()

The following examples show how to use android.widget.EditText#getSelectionStart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MarkDownProvider.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
public static void addQuote(@NonNull EditText editText) {
    String source = editText.getText().toString();
    int selectionStart = editText.getSelectionStart();
    int selectionEnd = editText.getSelectionEnd();
    String substring = source.substring(selectionStart, selectionEnd);
    String result;
    if (hasNewLine(source, selectionStart)) {
        result = "> " + substring;
    } else {
        result = "\n> " + substring;

    }
    editText.getText().replace(selectionStart, selectionEnd, result);
    editText.setSelection(result.length() + selectionStart);

}
 
Example 2
Source File: AutoCompleteProvider.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * " Search back from the cursor position till meeting '{' or ';'.
 * " '{' means statement start, ';' means end of a previous statement.
 *
 * @return statement before cursor
 * " Note: It's the base for parsing. And It's OK for most cases.
 */
@NonNull
private String getStatement(EditText editor) {
    String lineBeforeCursor = getCurrentLine(editor);
    if (lineBeforeCursor.matches("^\\s*(import|package)\\s+")) {
        return lineBeforeCursor;
    }
    int oldCursor = editor.getSelectionStart();
    int newCursor = oldCursor;
    while (true) {
        if (newCursor == 0) break;
        char c = editor.getText().charAt(newCursor);
        if (c == '{' || c == '}' || c == ';') {
            newCursor++;
            break;
        }
        newCursor--;
    }
    String statement = editor.getText().subSequence(newCursor, oldCursor).toString();
    return mergeLine(statement);
}
 
Example 3
Source File: SourceEditActivity.java    From MyBookshelf with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void sendText(@NotNull String txt) {
    if (isEmpty(txt)) return;
    View view = getWindow().getDecorView().findFocus();
    if (view instanceof EditText) {
        EditText editText = (EditText) view;
        int start = editText.getSelectionStart();
        int end = editText.getSelectionEnd();
        Editable edit = editText.getEditableText();//获取EditText的文字
        if (start < 0 || start >= edit.length()) {
            edit.append(txt);
        } else {
            edit.replace(start, end, txt);//光标所在位置插入文字
        }
    }
}
 
Example 4
Source File: EditorView.java    From Nimbus with GNU General Public License v3.0 6 votes vote down vote up
private void onBackPress(EditText editText) {
    int selection = editText.getSelectionStart();
    if (selection == 0) {
        int viewIndex = allLayout.indexOfChild(editText);
        View v = allLayout.getChildAt(viewIndex - 1);

        if (v != null) {
            if (v instanceof EditText) {
                if ((int) v.getTag() != 1) {
                    String s1 = editText.getText().toString();
                    EditText q = (EditText) v;
                    String s2 = q.getText().toString();
                    allLayout.removeView(editText);
                    q.setText(s1 + s2);
                    q.requestFocus();
                    q.setSelection(s2.length(), s2.length());
                    lastEditText = q;
                }
            } else if (v instanceof ImageView) {
                allLayout.removeView(v);
            }

        }
    }

}
 
Example 5
Source File: EditMicroBlogEmotionItemClickListener.java    From YiBo with Apache License 2.0 6 votes vote down vote up
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
		long id) {
	BaseAdapter adapter = (BaseAdapter)parent.getAdapter();
	String emotion = (String)adapter.getItem(position);
       if (StringUtil.isEmpty(emotion)) {
       	return;
       }
       
	EditText etText = (EditText)((Activity)context).findViewById(R.id.etText);
	int currentPos = 0;
	if (etText != null) {
		currentPos = etText.getSelectionStart();
		etText.getText().insert(currentPos, emotion);
	}

}
 
Example 6
Source File: EditTimerActivity.java    From ClockPlus with GNU General Public License v3.0 5 votes vote down vote up
@OnClick(R.id.backspace)
    void backspace() {
        if (mFocusGrabber.isFocused()) {
            mEditFieldsLayout.focusSearch(mFocusGrabber, View.FOCUS_LEFT).requestFocus();
        }
        EditText field = getFocusedField();
        if (field == null)
            return;
        int at = field.getSelectionStart();
        if (at == 0) {
            // At the beginning of current field, so move focus
            // to the preceding field
            View prev = field.focusSearch(View.FOCUS_LEFT);
            if (null == prev) {
                // Reached the beginning of the hours field
                return;
            }
            if (prev.requestFocus()) {
                if (prev instanceof EditText) {
                    // Always move the cursor to the end when moving focus back
                    ((EditText) prev).setSelection(FIELD_LENGTH);
                }
                // Recursively backspace on the newly focused field
                backspace();
            }
        } else {
            field.getText().replace(at - 1, at, "0");
            field.setSelection(at - 1);
//            updateStartButtonVisibility();
        }
    }
 
Example 7
Source File: SpanTextHelper.java    From YCCustomText with Apache License 2.0 5 votes vote down vote up
/**
 * 修改加粗斜体样式
 */
public void boldItalic(EditText lastFocusEdit) {
    Editable editable = lastFocusEdit.getEditableText();
    int start = lastFocusEdit.getSelectionStart();
    int end = lastFocusEdit.getSelectionEnd();
    HyperLogUtils.i("boldItalic select  Start:" + start + "   end:  " + end);
    if (checkNormalStyle(start, end)) {
        return;
    }
    new BoldItalicStyle().applyStyle(editable, start, end);
}
 
Example 8
Source File: SimpleCommonUtils.java    From aurora-imui with MIT License 5 votes vote down vote up
public static EmoticonClickListener getCommonEmoticonClickListener(final EditText editText) {
    return new EmoticonClickListener() {
        @Override
        public void onEmoticonClick(Object o, int actionType, boolean isDelBtn) {
            if (isDelBtn) {
                SimpleCommonUtils.delClick(editText);
            } else {
                if (o == null) {
                    return;
                }
                if (actionType == Constants.EMOTICON_CLICK_TEXT) {
                    String content = null;
                    if (o instanceof EmojiBean) {
                        content = ((EmojiBean) o).emoji;
                    } else if (o instanceof EmoticonEntity) {
                        content = ((EmoticonEntity) o).getContent();
                    }

                    if (TextUtils.isEmpty(content)) {
                        return;
                    }
                    int index = editText.getSelectionStart();
                    Editable editable = editText.getText();
                    editable.insert(index, content);
                }
            }
        }
    };
}
 
Example 9
Source File: SpanTextHelper.java    From YCCustomText with Apache License 2.0 5 votes vote down vote up
/**
 * 修改加粗样式
 */
public void bold(EditText lastFocusEdit) {
    //获取editable对象
    Editable editable = lastFocusEdit.getEditableText();
    //获取当前选中的起始位置
    int start = lastFocusEdit.getSelectionStart();
    //获取当前选中的末尾位置
    int end = lastFocusEdit.getSelectionEnd();
    HyperLogUtils.i("bold select  Start:" + start + "   end:  " + end);
    if (checkNormalStyle(start, end)) {
        return;
    }
    new BoldStyle().applyStyle(editable, start, end);
}
 
Example 10
Source File: MarkDownProvider.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
public static void addHeader(@NonNull EditText editText, int level) {
    String source = editText.getText().toString();
    int selectionStart = editText.getSelectionStart();
    int selectionEnd = editText.getSelectionEnd();
    StringBuilder result = new StringBuilder();
    String substring = source.substring(selectionStart, selectionEnd);
    if (!hasNewLine(source, selectionStart))
        result.append("\n");
    IntStream.range(0, level).forEach(integer -> result.append("#"));
    result.append(" ").append(substring);
    editText.getText().replace(selectionStart, selectionEnd, result.toString());
    editText.setSelection(selectionStart + result.length());

}
 
Example 11
Source File: EditTextSelectionState.java    From quill with MIT License 5 votes vote down vote up
public EditTextSelectionState(@NonNull EditText editText) {
    mEditText = editText;
    mFocused = editText.hasFocus();
    int selectionStart = editText.getSelectionStart();
    int selectionEnd = editText.getSelectionEnd();
    if (selectionStart > selectionEnd && selectionStart != -1 && selectionEnd != -1) {
        mSelectionStart = selectionEnd;
        mSelectionEnd = selectionStart;
    } else {
        mSelectionStart = selectionStart;
        mSelectionEnd = selectionEnd;
    }
}
 
Example 12
Source File: EditorFragment.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
private void markdownCode() {
    final EditText et = mContentET;
    Editable editable = et.getText();
    int start = et.getSelectionStart();
    if (et.hasSelection()) {
        int end = et.getSelectionEnd();
        editable.insert(end, CODE);
        editable.insert(start, CODE);
        et.setSelection(start + 1, end + 1);
    } else {
        editable.insert(start, CODE2);
        et.setSelection(start + 1);
    }
}
 
Example 13
Source File: EditorFragment.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
private void quotePost(String raw) {

        final EditText et = mContentET;
        Editable editable = et.getText();
        int start = et.getSelectionStart();
        editable.insert(start, String.format(QUOTE_POST, mUsername, mPostNum, mTopic.id, mPostRaw));
    }
 
Example 14
Source File: SpanTextHelper.java    From YCCustomText with Apache License 2.0 5 votes vote down vote up
/**
 * 修改删除线样式
 */
public void strikeThrough(EditText lastFocusEdit) {
    Editable editable = lastFocusEdit.getEditableText();
    int start = lastFocusEdit.getSelectionStart();
    int end = lastFocusEdit.getSelectionEnd();
    HyperLogUtils.i("strikeThrough select  Start:" + start + "   end:  " + end);
    if (checkNormalStyle(start, end)) {
        return;
    }
    new StrikeThroughStyle().applyStyle(editable, start, end);
}
 
Example 15
Source File: L3PostActivity.java    From smart-farmer-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onFaceItemClick(FacePanelView view, String face, int faceId) {
    View focusView = getCurrentFocus();
    if (focusView != null && focusView instanceof EditText) {
        EditText editText = (EditText) focusView;
        int index = editText.getSelectionStart();
        if (FacePanelView.KEY_DELETE.equals(face)) {
            //发送删除事件
            if (index > 0) {
                editText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
            }
        } else {
            face = "{:" + face + ":}";
            int size = SizeUtils.dp2px(20);
            Drawable drawable = getResources().getDrawable(faceId);
            drawable.setBounds(0, 0, size, size);
            ImageSpan imageSpan = new ImageSpan(drawable, ALIGN_BOTTOM);
            SpannableString spannableString = new SpannableString(face);
            spannableString.setSpan(imageSpan, 0, face.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            if (index < 0 || index >= editText.getText().length()) {
                editText.getEditableText().append(spannableString);
            } else {
                editText.getEditableText().insert(index, spannableString);
            }

            editText.setSelection(index + face.length());
        }
    }
}
 
Example 16
Source File: CommentEditor.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
public final void formatSelectedText(EditText commentView, int what) {
	int start = commentView.getSelectionStart();
	int end = commentView.getSelectionEnd();
	if (start == -1 || end == -1) {
		return;
	}
	FormatResult result = formatSelectedText(commentView.getText(), what, start, end);
	if (result != null) {
		commentView.setSelection(result.start, result.end);
	}
}
 
Example 17
Source File: ResetPwdFragment.java    From tysq-android with GNU General Public License v3.0 5 votes vote down vote up
private void setPwdState(ImageView imageView,
                         EditText editText,
                         boolean isHidePwd) {
    int start = editText.getSelectionStart();

    if (isHidePwd) {
        imageView.setImageDrawable(hidePwdDrawable);
        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
    } else {
        imageView.setImageDrawable(showPwdDrawable);
        editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
    }

    editText.setSelection(start);
}
 
Example 18
Source File: SpanTextHelper.java    From YCCustomText with Apache License 2.0 5 votes vote down vote up
/**
 * 修改斜体样式
 */
public void italic(EditText lastFocusEdit) {
    Editable editable = lastFocusEdit.getEditableText();
    int start = lastFocusEdit.getSelectionStart();
    int end = lastFocusEdit.getSelectionEnd();
    HyperLogUtils.i("italic select  Start:" + start + "   end:  " + end);
    if (checkNormalStyle(start, end)) {
        return;
    }
    new ItalicStyle().applyStyle(editable, start, end);
}
 
Example 19
Source File: Selection.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
public Selection(EditText editor) {
    this(editor.getSelectionStart(), editor.getSelectionEnd());
}
 
Example 20
Source File: RegisterActivity.java    From FimiX8-RE with MIT License 4 votes vote down vote up
private int getEditTextCursorIndex(EditText mEditText) {
    return mEditText.getSelectionStart();
}