本文整理汇总了Java中android.text.Editable.replace方法的典型用法代码示例。如果您正苦于以下问题:Java Editable.replace方法的具体用法?Java Editable.replace怎么用?Java Editable.replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.text.Editable
的用法示例。
在下文中一共展示了Editable.replace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: formatColor
import android.text.Editable; //导入方法依赖的package包/类
private static Editable formatColor(Spannable format, String... args)
{
Editable text = editableFactory.newEditable(format);
for(int i = 0; i < text.length(); )
{
if(text.charAt(i) == '$' && i + 1 < text.length())
{
if(Character.isDigit(text.charAt(i + 1)))
{
int digit = Character.digit(text.charAt(i + 1), 10);
Spannable arg = digit < args.length ? IRCFormatting.parse(args[digit]) : new SpannableString("");
text.replace(i, i + 2, arg);
i += arg.length();
continue;
}
if(text.charAt(i + 1) == '$')
{
text.replace(i, i + 2, "$");
i++;
continue;
}
}
i++;
}
return text;
}
示例2: afterTextChanged
import android.text.Editable; //导入方法依赖的package包/类
@Override
public void afterTextChanged(Editable s) {
if (countryFormatter == null)
return;
if (TextUtils.isEmpty(s))
return;
countryFormatter.clear();
String number = s.toString().replaceAll("[^\\d.]", "");
String formattedNumber = null;
for (int i=0;i<number.length();i++) {
formattedNumber = countryFormatter.inputDigit(number.charAt(i));
}
if (formattedNumber != null && !s.toString().equals(formattedNumber)) {
s.replace(0, s.length(), formattedNumber);
}
}
示例3: undo
import android.text.Editable; //导入方法依赖的package包/类
public void undo() {
EditItem edit = mEditHistory.getPrevious();
if (edit == null) {
return;
}
Editable editable = mTextView.getEditableText();
int start = edit.start;
int end = start + (edit.after != null ? edit.after.length() : 0);
mIsUndoOrRedo = true;
editable.replace(start, end, edit.before);
mIsUndoOrRedo = false;
for (Object o : editable.getSpans(0, editable.length(), UnderlineSpan.class)) {
editable.removeSpan(o);
}
Selection.setSelection(editable, edit.before == null ? start : (start + edit.before.length()));
}
示例4: redo
import android.text.Editable; //导入方法依赖的package包/类
public void redo() {
EditItem edit = mEditHistory.getNext();
if (edit == null) {
return;
}
Editable text = mTextView.getEditableText();
int start = edit.start;
int end = start + (edit.before != null ? edit.before.length() : 0);
mIsUndoOrRedo = true;
text.replace(start, end, edit.after);
mIsUndoOrRedo = false;
// This will get rid of underlines inserted when editor tries to come
// up with a suggestion.
for (Object o : text.getSpans(0, text.length(), UnderlineSpan.class)) {
text.removeSpan(o);
}
Selection.setSelection(text, edit.after == null ? start
: (start + edit.after.length()));
}
示例5: undo
import android.text.Editable; //导入方法依赖的package包/类
public void undo() {
EditItem edit = mEditHistory.getPrevious();
if (edit == null) {
return;
}
Editable editable = mTextView.getEditableText();
int start = edit.start;
int end = start + (edit.after != null ? edit.after.length() : 0);
mIsUndoOrRedo = true;
editable.replace(start, end, edit.before);
mIsUndoOrRedo = false;
for (Object o : editable.getSpans(0, editable.length(), UnderlineSpan.class)) {
editable.removeSpan(o);
}
Selection.setSelection(editable,
edit.before == null ? start : (start + edit.before.length()));
}
示例6: updateContext
import android.text.Editable; //导入方法依赖的package包/类
/**
* 更新编辑框中的内容
*
* @param editable
* @param values
*/
private void updateContext(Editable editable, String values) {
if (spaceType == SpaceType.IDCardNumberType) {
editable.replace(0, editable.length(), values);
} else {
editText.setText(values);
try {
editText.setSelection(location);
} catch (Exception e) {
e.printStackTrace();
}
}
}
示例7: insertTextToEditText
import android.text.Editable; //导入方法依赖的package包/类
private void insertTextToEditText(String txt) {
if (TextUtils.isEmpty(txt)) return;
int start = mEditText.getSelectionStart();
int end = mEditText.getSelectionEnd();
Editable edit = mEditText.getEditableText();//获取EditText的文字
if (start < 0 || start >= edit.length()) {
edit.append(txt);
} else {
edit.replace(start, end, txt);//光标所在位置插入文字
}
}
示例8: replaceLastChar
import android.text.Editable; //导入方法依赖的package包/类
private void replaceLastChar(@NonNull String chr, SpannableString spannable) {
Editable msg = getText();
int selStart = getSelectionStart();
int selEnd = getSelectionEnd();
int selStartBefore = selStart - 1;
if (selStart == selEnd && selStart > 0
&& chr.equals(msg.subSequence(selStartBefore, selEnd).toString())
&& msg.getSpans(selStartBefore, selEnd, RichEditText.TagSpan.class).length == 0) {
selStart = selStartBefore;
}
msg.replace(selStart >= 0 ? selStart : 0, selEnd >= 0 ? selEnd : 0, spannable);
}
示例9: onTopicKeyArrived
import android.text.Editable; //导入方法依赖的package包/类
@Override
public boolean onTopicKeyArrived(RichEditText editText) {
Editable msg = editText.getText();
String msgStr = msg.toString();
int selStartIndex = editText.getSelectionStart();
int selEndIndex = editText.getSelectionEnd();
if (TextUtils.isEmpty(msgStr.trim()) || selStartIndex <= 0) {
skipTopic(editText);
return true;
}
int startIndex = 0;
RichEditText.TagSpan[] spans = msg.getSpans(0, selStartIndex, RichEditText.TagSpan.class);
if (spans.length > 0) {
startIndex = msg.getSpanEnd(spans[spans.length - 1]);
}
boolean isMatcher = false;
String tagStr = msgStr.substring(startIndex, selStartIndex) + "#";
Pattern pattern = Pattern.compile(RichEditText.MATCH_TOPIC);
Matcher matcher = pattern.matcher(tagStr);
while (matcher.find()) {
String str = matcher.group();
int matcherStart = matcher.start() + startIndex;
int matcherEnd = matcher.end() + startIndex;
if (matcherEnd == selStartIndex + 1)
msg.replace(selStartIndex, selEndIndex, "#");
msg.setSpan(new RichEditText.TagSpan(str), matcherStart, matcherEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
isMatcher = true;
}
if (isMatcher) {
editText.setSelection(selEndIndex);
return false;
}
skipTopic(editText);
return true;
}
示例10: setupMentionsAutocomplete
import android.text.Editable; //导入方法依赖的package包/类
private void setupMentionsAutocomplete() {
EditText edit = (EditText) findViewById(R.id.multi);
float elevation = 6f;
Drawable backgroundDrawable = new ColorDrawable(Color.WHITE);
AutocompletePolicy policy = new CharPolicy('@'); // Look for @mentions
AutocompletePresenter<User> presenter = new UserPresenter(this);
AutocompleteCallback<User> callback = new AutocompleteCallback<User>() {
@Override
public boolean onPopupItemClicked(Editable editable, User item) {
// Replace query text with the full name.
int[] range = CharPolicy.getQueryRange(editable);
if (range == null) return false;
int start = range[0];
int end = range[1];
String replacement = item.getUsername();
editable.replace(start, end, replacement);
// This is better done with regexes and a TextWatcher, due to what happens when
// the user clears some parts of the text. Up to you.
editable.setSpan(new StyleSpan(Typeface.BOLD), start, start+replacement.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return true;
}
public void onPopupVisibilityChanged(boolean shown) {}
};
mentionsAutocomplete = Autocomplete.<User>on(edit)
.with(elevation)
.with(backgroundDrawable)
.with(policy)
.with(presenter)
.with(callback)
.build();
}
示例11: insertAtCursor
import android.text.Editable; //导入方法依赖的package包/类
private static int insertAtCursor(EditTextSelectionState selectionState,
CharSequence textToInsert) {
EditText editText = selectionState.getEditText();
Editable editable = editText.getText();
int editableLen = editable.length();
int selStart = selectionState.getSelectionStart();
int selEnd = selectionState.getSelectionEnd();
int start = (selStart >= 0) ? selStart : editableLen-1;
int end = (selEnd >= 0) ? selEnd : editableLen-1;
editable.replace(Math.min(start, end), Math.max(start, end),
textToInsert, 0, textToInsert.length());
return Math.min(start, end);
}
示例12: undo
import android.text.Editable; //导入方法依赖的package包/类
/**
* Perform undo.
*/
public void undo() {
EditItem edit = mEditHistory.getPrevious();
if (edit == null) {
return;
}
Editable text = getEditableText();
int start = edit.mmStart;
int end = start + (edit.mmAfter != null
? edit.mmAfter.length() : 0);
mIsUndoOrRedo = true;
text.replace(start, end, edit.mmBefore);
mIsUndoOrRedo = false;
// This will get rid of underlines inserted when editor tries to come
// up with a suggestion.
for (Object o : text.getSpans(0,
text.length(), UnderlineSpan.class)) {
text.removeSpan(o);
}
Selection.setSelection(text,
edit.mmBefore == null ? start
: (start + edit.mmBefore.length()));
}
示例13: redo
import android.text.Editable; //导入方法依赖的package包/类
/**
* Perform redo.
*/
public void redo() {
EditItem edit = mEditHistory.getNext();
if (edit == null) {
return;
}
Editable text = getEditableText();
int start = edit.mmStart;
int end = start + (edit.mmBefore != null
? edit.mmBefore.length() : 0);
mIsUndoOrRedo = true;
text.replace(start, end, edit.mmAfter);
mIsUndoOrRedo = false;
// This will get rid of underlines inserted when editor tries to come
// up with a suggestion.
for (Object o : text.getSpans(0,
text.length(), UnderlineSpan.class)) {
text.removeSpan(o);
}
Selection.setSelection(text,
edit.mmAfter == null ? start
: (start + edit.mmAfter.length()));
}
示例14: onEmojiSelected
import android.text.Editable; //导入方法依赖的package包/类
/**
* *************** IEmojiSelectedListener ***************
*/
@Override
public void onEmojiSelected(String key) {
Editable mEditable = messageEditText.getText();
if (key.equals("/DEL")) {
messageEditText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
} else {
int start = messageEditText.getSelectionStart();
int end = messageEditText.getSelectionEnd();
start = (start < 0 ? 0 : start);
end = (start < 0 ? 0 : end);
mEditable.replace(start, end, key);
}
}
示例15: afterTextChanged
import android.text.Editable; //导入方法依赖的package包/类
@Override
public void afterTextChanged(Editable s) {
removeTextChangedListener(this);
int length = s.length();
int nextGroupStart = 4;
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
if (i == nextGroupStart) {
if (c != SPACING_CHAR) {
if (!Character.isDigit(c)) {
s.replace(i, i + 1, spacingString);
} else {
s.insert(i, spacingString);
length = s.length();
if (deleted) {
int selectionStart = getSelectionStart();
int selectionEnd = getSelectionEnd();
int newSelectionStart = selectionStart - 1 == i ? selectionStart - 1 : selectionStart;
int newSelectionEnd = selectionEnd - 1 == i ? selectionEnd - 1 : selectionEnd;
setSelection(newSelectionStart, newSelectionEnd);
}
}
}
nextGroupStart += 5;
} else {
if (!Character.isDigit(c)) {
s.delete(i, i + 1);
length = s.length();
}
}
}
addTextChangedListener(this);
int cvcLength = CVCEditText.CVC_MAX_LENGTH;
List<String> allowedCardsList = new ArrayList<>(allowedCardTypes.keySet());
CardType cardType = CardType.detect(s.toString().replace(" ", ""), allowedCardsList);
if (cardType == CardType.amex) {
cvcLength = CVCEditText.CVC_MAX_LENGTH_AMEX;
}
if (cardType != CardType.UNKNOWN) {
if (!StringUtils.isEmptyOrNull(baseURL)) {
final String imageUrl = IconUtil.addScaleFactorToIconUrl(getContext(), baseURL + cardType + ".png");
AsyncImageDownloader.downloadImage(getContext(), new AsyncImageDownloader.ImageListener() {
@Override
public void onImage(final Bitmap bitmap, final String url) {
setCCIcon(bitmap);
}
}, imageUrl, null);
}
numberOfDigits = cardType.getNumberOfDigits();
} else {
initializeLogo();
numberOfDigits = null;
}
if (cvcEditText != null) {
for (CVCFieldStatusListener cvcFieldStatusListener : cvcFieldStatusListeners) {
cvcFieldStatusListener.onCVCFieldStatusChanged(allowedCardTypes.get(cardType.name()));
}
cvcEditText.setMaxLength(cvcLength);
}
if (validator != null) {
validator.setReady(CreditCardEditText.this, isValidNr(s.toString()));
}
}