android.text.Editable Java Examples

The following examples show how to use android.text.Editable. 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: KeyboardKeyListener.java    From BluetoothHidEmu with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKeyDown(View view, Editable content, int keyCode, KeyEvent event) {
    switch (keyCode) {
    
    case KeyEvent.KEYCODE_ENTER:
        TextKeyListener.clear(content);
        mHidPayload.assemblePayload(HidKeyPair.ENTER);
        mSocketManager.sendPayload(mHidPayload);
        return true;
    case KeyEvent.KEYCODE_DEL:
    	mHidPayload.assemblePayload(HidKeyPair.DEL);
        mSocketManager.sendPayload(mHidPayload);
    case KeyEvent.KEYCODE_VOLUME_UP:
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        mHidPayload.assemblePayload(keyCode);
        mSocketManager.sendPayload(mHidPayload);
    default:
        return mTextKeyListener.onKeyDown(view, content, keyCode, event);
    }
}
 
Example #2
Source File: TextChipsEditView.java    From talk-android with MIT License 6 votes vote down vote up
/**
 * Handles pasting a {@link ClipData} to this {@link TextChipsEditView}.
 */
private void handlePasteClip(ClipData clip) {
    removeTextChangedListener(mTextWatcher);

    if (clip != null && clip.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
        for (int i = 0; i < clip.getItemCount(); i++) {
            CharSequence paste = clip.getItemAt(i).getText();
            if (paste != null) {
                int start = getSelectionStart();
                int end = getSelectionEnd();
                Editable editable = getText();
                if (start >= 0 && end >= 0 && start != end) {
                    editable.append(paste, start, end);
                } else {
                    editable.insert(end, paste);
                }
                handlePasteAndReplace();
            }
        }
    }

    mHandler.post(mAddTextWatcher);
}
 
Example #3
Source File: Html.java    From ForPDA with GNU General Public License v3.0 6 votes vote down vote up
private static void startBlockElement(Editable text, Attributes attributes, int margin) {
    final int len = text.length();
    if (margin > 0) {
        appendNewlines(text, margin);
        start(text, new Newline(margin));
    }
    String style = attributes.getValue("", "style");
    if (style != null) {
        Matcher m = getTextAlignPattern().matcher(style);
        if (m.find()) {
            String alignment = m.group(1);
            if (alignment.equalsIgnoreCase("start")) {
                start(text, new Alignment(Layout.Alignment.ALIGN_NORMAL));
            } else if (alignment.equalsIgnoreCase("center")) {
                start(text, new Alignment(Layout.Alignment.ALIGN_CENTER));
            } else if (alignment.equalsIgnoreCase("end")) {
                start(text, new Alignment(Layout.Alignment.ALIGN_OPPOSITE));
            }
        }
    }
}
 
Example #4
Source File: Main.java    From iZhihu with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void afterTextChanged(Editable editable) {
    searchString = editable.toString().trim();

    if (searchString != null && searchString.length() > 0) {
        new SearchQuestionTask(context, new SearchQuestionTask.Callback() {
            @Override
            public void onPreExecute() {
                // ...
            }

            @Override
            public void onPostExecute(ArrayList<Question> result) {
                searchedQuestions.clear();
                searchedQuestions.addAll(result);
                if (questionsAdapter != null) {
                    questionsAdapter.notifyDataSetChanged();
                }
            }
        }).execute(searchString);
    }
}
 
Example #5
Source File: SafePasteEditText.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTextContextMenuItem(int id) {
	if (id == android.R.id.paste) {
		Editable editable = getEditableText();
		InputFilter[] filters = editable.getFilters();
		InputFilter[] tempFilters = new InputFilter[filters != null ? filters.length + 1 : 1];
		if (filters != null) {
			System.arraycopy(filters, 0, tempFilters, 1, filters.length);
		}
		tempFilters[0] = SPAN_FILTER;
		editable.setFilters(tempFilters);
		try {
			return super.onTextContextMenuItem(id);
		} finally {
			editable.setFilters(filters);
		}
	} else {
		return super.onTextContextMenuItem(id);
	}
}
 
Example #6
Source File: RichEditText.java    From RichEditText with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param bitmap
 * @param filePath
 * @param start
 * @param end
 */
public void addImage(Bitmap bitmap, String filePath,int start,int end) {
	Log.i("imgpath", filePath);
	String pathTag = "<img src=\"" + filePath + "\"/>";
	SpannableString spanString = new SpannableString(pathTag);
	// 获取屏幕的宽高
	int paddingLeft = getPaddingLeft();
	int paddingRight = getPaddingRight();
	int bmWidth = bitmap.getWidth();//图片高度
	int bmHeight = bitmap.getHeight();//图片宽度
	int zoomWidth =  getWidth() - (paddingLeft + paddingRight);
	int zoomHeight = (int) (((float)zoomWidth / (float)bmWidth) * bmHeight);
	Bitmap newBitmap = zoomImage(bitmap, zoomWidth,zoomHeight);
	ImageSpan imgSpan = new ImageSpan(mContext, newBitmap);
	spanString.setSpan(imgSpan, 0, pathTag.length(),
			Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
	Editable editable = this.getText(); // 获取edittext内容
	editable.delete(start, end);//删除
	editable.insert(start, spanString); // 设置spanString要添加的位置
}
 
Example #7
Source File: BaseViewController.java    From android-pin with MIT License 6 votes vote down vote up
private void initKeyboard() {
    mKeyboardView.setOnKeyboardActionListener(new PinKeyboardView.PinPadActionListener() {
        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Editable e = mPinputView.getText();
            if (primaryCode == PinKeyboardView.KEYCODE_DELETE) {
                int len = e.length();
                if (len == 0) {
                    return;
                }
                e.delete(len - 1, e.length());
            } else {
                mPinputView.getText().append((char) primaryCode);
            }
        }
    });
    mKeyboardView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == ACTION_DOWN && getConfig().shouldVibrateOnKey()) {
                VibrationHelper.vibrate(mContext, getConfig().vibrateDuration());
            }
            return false;
        }
    });
}
 
Example #8
Source File: EditTextUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 追加内容
 * @param editText {@link EditText}
 * @param content  文本内容
 * @param start    开始添加的位置
 * @param isSelect 是否设置光标
 * @param <T>      泛型
 * @return {@link EditText}
 */
public static <T extends EditText> T insert(final T editText, final CharSequence content, final int start, final boolean isSelect) {
    if (editText != null && !TextUtils.isEmpty(content)) {
        try {
            Editable editable = editText.getText();
            // 在指定位置 追加内容
            editable.insert(start, content);
            // 设置光标
            if (isSelect) {
                editText.setSelection(editText.getText().toString().length());
            }
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "insert");
        }
    }
    return editText;
}
 
Example #9
Source File: ListTagHandler.java    From zulip-android with Apache License 2.0 6 votes vote down vote up
/**
 * Closes a list item.
 *
 * @param text
 * @param indentation
 */
public final void closeItem(final Editable text, final int indentation) {
    if (text.length() > 0 && text.charAt(text.length() - 1) != '\n') {
        text.append("\n");
    }
    final Object[] replaces = getReplaces(text, indentation);
    final int len = text.length();
    final ListTag listTag = getLast(text);
    final int where = text.getSpanStart(listTag);
    text.removeSpan(listTag);
    if (where != len) {
        for (Object replace : replaces) {
            text.setSpan(replace, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example #10
Source File: BaseInputConnection.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * The default implementation returns the text currently selected, or null if none is
 * selected.
 */
public CharSequence getSelectedText(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 == b || a < 0) return null;

    if ((flags&GET_TEXT_WITH_STYLES) != 0) {
        return content.subSequence(a, b);
    }
    return TextUtils.substring(content, a, b);
}
 
Example #11
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 #12
Source File: ViewerActivityController.java    From document-viewer with GNU General Public License v3.0 5 votes vote down vote up
@ActionMethod(ids = R.id.actions_addBookmark)
public void addBookmark(final ActionEx action) {
    final Editable value = action.getParameter("input");
    final String name = value.toString();
    final Page page = documentModel.getCurrentPageObject();
    if (page != null) {
        final ViewState state = ViewState.get(getDocumentController());
        final PointF pos = state.getPositionOnPage(page);
        bookSettings.bookmarks.add(new Bookmark(name, documentModel.getCurrentIndex(), pos.x, pos.y));
        Collections.sort(bookSettings.bookmarks);
        SettingsManager.storeBookSettings(bookSettings);
        UIManagerAppCompat.invalidateOptionsMenu(getManagedComponent());
        state.release();
    }
}
 
Example #13
Source File: ImeUtils.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * @param editable The editable.
 * @return Debug string for the given {@Editable}.
 */
static String getEditableDebugString(Editable editable) {
    return String.format(Locale.US, "Editable {[%s] SEL[%d %d] COM[%d %d]}",
            editable.toString(), Selection.getSelectionStart(editable),
            Selection.getSelectionEnd(editable),
            BaseInputConnection.getComposingSpanStart(editable),
            BaseInputConnection.getComposingSpanEnd(editable));
}
 
Example #14
Source File: ReactEditText.java    From react-native-GPay with MIT License 5 votes vote down vote up
@Override
public void afterTextChanged(Editable s) {
  if (!mIsSettingTextFromJS && mListeners != null) {
    for (TextWatcher listener : mListeners) {
      listener.afterTextChanged(s);
    }
  }
}
 
Example #15
Source File: EditorFragment.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get current cursor line
 */
public int getCurrentCursorLine(Editable editable) {
    int selectionStartPos = Selection.getSelectionStart(editable);

    // no selection
    if (selectionStartPos < 0) return -1;

    String preSelectionStartText = editable.toString().substring(0, selectionStartPos);
    return StringUtils.countMatches(preSelectionStartText, "\n");
}
 
Example #16
Source File: Html.java    From tysq-android with GNU General Public License v3.0 5 votes vote down vote up
private static void endHeading(Editable text) {
        // RelativeSizeSpan and StyleSpan are CharacterStyles
        // Their ranges should not include the newlines at the end
        Heading h = getLast(text, Heading.class);
        if (h != null) {

            JerryHeadSpan jerryHeadSpan;

            switch (h.mLevel) {
                case 0:
                    jerryHeadSpan = new JerryH1Span();
                    break;
                case 1:
                    jerryHeadSpan = new JerryH2Span();
                    break;
                case 2:
                    jerryHeadSpan = new JerryH3Span();
                    break;
                case 3:
                    jerryHeadSpan = new JerryH4Span();
                    break;
                default:
                    jerryHeadSpan = new JerryH4Span();
                    break;

            }
            setSpanFromMark(text, h, jerryHeadSpan);

//            setSpanFromMark(text, h, new RelativeSizeSpan(HEADING_SIZES[h.mLevel]),
//                    new StyleSpan(Typeface.BOLD));
        }

        endBlockElement(text);
    }
 
Example #17
Source File: RichUtils.java    From RichEditor with MIT License 5 votes vote down vote up
/**
 * 删除回车之后需要处理两行的block span合并,修改成第一行的block样式
 */
void mergeBlockSpanAfterDeleteEnter() {
    Editable editable = mRichEditText.getEditableText();
    int[] curBlockBoundary = getCursorPosBlockBoundary();
    int start = curBlockBoundary[0];
    int end = curBlockBoundary[1];
    // 合并前第一行的block span
    IBlockSpan[] firstBlockSpans = editable.getSpans(start, start, IBlockSpan.class);
    // 合并后的block span
    IBlockSpan[] mergeBlockSpans = editable.getSpans(start, end, IBlockSpan.class);

    // 清空合并后的block span
    for (IBlockSpan blockSpan : mergeBlockSpans) {
        editable.removeSpan(blockSpan);
    }

    if (firstBlockSpans.length <= 0) {
        return;
    }

    String blockType = firstBlockSpans[0].getType();
    Class spanClazz = getSpanClassFromType(blockType);

    editable.setSpan(getBlockSpan(spanClazz), start, end, getBlockSpanFlag(blockType));
    mRichEditText.setCursorHeight(getCursorHeight(blockType));
    setOtherBlockStyleBtnDisable(blockType);
}
 
Example #18
Source File: NoteEditText.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
public String getText() {
    Editable editable = mEditText.getText();
    if (editable == null) {
        return "";
    }
    return editable.toString();
}
 
Example #19
Source File: CaseStepNodeAdapter.java    From SoloPi with Apache License 2.0 5 votes vote down vote up
@Override
public void afterTextChanged(Editable s) {
    boolean result = updateNodeProperty(key, s.toString(), node);
    if (!result) {
        infoText.setText(R.string.node__invalid_param);
    } else {
        infoText.setText("");
    }
}
 
Example #20
Source File: Database.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Changes the passphrase associated with this database. The supplied
 * Editable is cleared as part of this operation.
 *
 * @param editor
 *            source of passphrase, presumably from a user
 */
public void rekey(Editable editor) {
	char[] passphrase = new char[editor.length()];

	editor.getChars(0, editor.length(), passphrase, 0);

	try {
		rekey(passphrase);
	} finally {
		editor.clear();
	}
}
 
Example #21
Source File: SearchView.java    From Material-SearchView with Apache License 2.0 5 votes vote down vote up
@Override
protected void onQueryTextChanged(@NonNull Editable s) {
    mQuery = s.toString();
    updateCloseVoiceState(mQuery);

    if (mOnQueryTextListener != null) {
        mOnQueryTextListener.onQueryTextChanged(mQuery);
    }
}
 
Example #22
Source File: CardCVVFragment.java    From CreditCardView with Apache License 2.0 5 votes vote down vote up
@Override
public void afterTextChanged(Editable s) {
    onEdit(s.toString());
    if (s.length() == mMaxCVV) {
        onComplete();
    }
}
 
Example #23
Source File: CardNumberFormat.java    From AndroidSDK with Apache License 2.0 5 votes vote down vote up
@Override
public void afterTextChanged(Editable s) {
  int dateExpireLength = editText.getText().toString().length();
  if (s.length() > 18 &&  dateExpireLength> 4) { enableView(); } else { disableView(); }
  if (lock || s.length() > 16) {
    return;
  }
  lock = true;
  for (int i = 4; i < s.length(); i += 5) {
    if (s.toString().charAt(i) != ' ') {
      s.insert(i, " ");
    }
  }
  lock = false;
}
 
Example #24
Source File: SpanHelper.java    From AwesomeValidation with MIT License 5 votes vote down vote up
public static void reset(EditText editText) {
    Editable editable = editText.getText();
    if (editable != null) {
        editable.clearSpans();
    }
    editText.setText(editable);
}
 
Example #25
Source File: SourceEditActivity.java    From a with GNU General Public License v3.0 5 votes vote down vote up
private void insertTextToEditText(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 #26
Source File: EditEntryActivity.java    From Aegis with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void afterTextChanged(Editable s) {
    if (!_hasCustomIcon) {
        TextDrawable drawable = TextDrawableHelper.generate(_textIssuer.getText().toString(), _textName.getText().toString(), _iconView);
        _iconView.setImageDrawable(drawable);
    }
}
 
Example #27
Source File: CategoryTextWatcher.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Helper function to remove all spans of a certain type from an {@link Editable}.
 */
private <T> void removeSpans(Editable text, Class<T> clazz) {
    T[] spans = text.getSpans(0, text.length(), clazz);
    for (T span : spans) {
        text.removeSpan(span);
    }
}
 
Example #28
Source File: AppRestrictionEnforcerFragment.java    From enterprise-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void afterTextChanged(Editable s) {
    try {
        String string = s.toString();
        if (!TextUtils.isEmpty(string)) {
            saveNumber(getActivity(), Integer.parseInt(string));
        }
    } catch (NumberFormatException e) {
        Toast.makeText(getActivity(), "Not an integer!", Toast.LENGTH_SHORT).show();
    }
}
 
Example #29
Source File: EditText.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public Editable getText() {
    CharSequence text = super.getText();
    // This can only happen during construction.
    if (text == null) {
        return null;
    }
    if (text instanceof Editable) {
        return (Editable) super.getText();
    }
    super.setText(text, BufferType.EDITABLE);
    return (Editable) super.getText();
}
 
Example #30
Source File: RepostWeiboWithAppSrcActivity.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
    String charSequence = mEditText.getText().toString();
    int count = Utility.length(charSequence);
    String text = count <= 0 ? getString(R.string.send_weibo) : count + "";
    weiTextCountTV.setText(text);
    if (count > 140) {
        weiTextCountTV.setTextColor(Color.RED);
    } else {
        weiTextCountTV.setTextColor(Color.BLACK);
    }
}