use of android.text.Spannable in project XobotOS by xamarin.
the class WebTextView method dispatchKeyEvent.
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.isSystem()) {
return super.dispatchKeyEvent(event);
}
// Treat ACTION_DOWN and ACTION MULTIPLE the same
boolean down = event.getAction() != KeyEvent.ACTION_UP;
int keyCode = event.getKeyCode();
boolean isArrowKey = false;
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
isArrowKey = true;
break;
}
if (KeyEvent.KEYCODE_TAB == keyCode) {
if (down) {
onEditorAction(EditorInfo.IME_ACTION_NEXT);
}
return true;
}
Spannable text = (Spannable) getText();
int oldStart = Selection.getSelectionStart(text);
int oldEnd = Selection.getSelectionEnd(text);
// changed, so send the events immediately.
if (KeyEvent.KEYCODE_DEL == keyCode) {
if (oldStart == 0 && oldEnd == 0) {
sendDomEvent(event);
return true;
}
if (down) {
mGotDelete = true;
mDelSelStart = oldStart;
mDelSelEnd = oldEnd;
}
}
if (mSingle && (KeyEvent.KEYCODE_ENTER == keyCode || KeyEvent.KEYCODE_NUMPAD_ENTER == keyCode)) {
if (isPopupShowing()) {
return super.dispatchKeyEvent(event);
}
if (!down) {
// Hide the keyboard, since the user has just submitted this
// form. The submission happens thanks to the two calls
// to sendDomEvent.
InputMethodManager.getInstance(mContext).hideSoftInputFromWindow(getWindowToken(), 0);
sendDomEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
sendDomEvent(event);
}
return super.dispatchKeyEvent(event);
} else if (KeyEvent.KEYCODE_DPAD_CENTER == keyCode) {
// Note that this handles center key and trackball.
if (isPopupShowing()) {
return super.dispatchKeyEvent(event);
}
// Center key should be passed to a potential onClick
if (!down) {
mWebView.centerKeyPressOnTextField();
}
// Pass to super to handle longpress.
return super.dispatchKeyEvent(event);
}
// Ensure there is a layout so arrow keys are handled properly.
if (getLayout() == null) {
measure(mWidthSpec, mHeightSpec);
}
int oldLength = text.length();
boolean maxedOut = mMaxLength != -1 && oldLength == mMaxLength;
// If we are at max length, and there is a selection rather than a
// cursor, we need to store the text to compare later, since the key
// may have changed the string.
String oldText;
if (maxedOut && oldEnd != oldStart) {
oldText = text.toString();
} else {
oldText = "";
}
if (super.dispatchKeyEvent(event)) {
// ahead and pass it down.
if (KeyEvent.KEYCODE_ENTER == keyCode || KeyEvent.KEYCODE_NUMPAD_ENTER == keyCode) {
// For multi-line text boxes, newlines will
// trigger onTextChanged for key down (which will send both
// key up and key down) but not key up.
mGotEnterDown = true;
}
if (maxedOut && !isArrowKey && keyCode != KeyEvent.KEYCODE_DEL) {
if (oldEnd == oldStart) {
// Return true so the key gets dropped.
return true;
} else if (!oldText.equals(getText().toString())) {
// FIXME: This makes the text work properly, but it
// does not pass down the key event, so it may not
// work for a textfield that has the type of
// behavior of GoogleSuggest. That said, it is
// unlikely that a site would combine the two in
// one textfield.
Spannable span = (Spannable) getText();
int newStart = Selection.getSelectionStart(span);
int newEnd = Selection.getSelectionEnd(span);
mWebView.replaceTextfieldText(0, oldLength, span.toString(), newStart, newEnd);
return true;
}
}
/* FIXME:
* In theory, we would like to send the events for the arrow keys.
* However, the TextView can arbitrarily change the selection (i.e.
* long press followed by using the trackball). Therefore, we keep
* in sync with the TextView via onSelectionChanged. If we also
* send the DOM event, we lose the correct selection.
if (isArrowKey) {
// Arrow key does not change the text, but we still want to send
// the DOM events.
sendDomEvent(event);
}
*/
return true;
}
// multiple newlines in the native textarea.
if (mGotEnterDown && !down) {
return true;
}
// if it is a navigation key, pass it to WebView
if (isArrowKey) {
// WebView check the trackballtime in onKeyDown to avoid calling
// native from both trackball and key handling. As this is called
// from WebTextView, we always want WebView to check with native.
// Reset trackballtime to ensure it.
mWebView.resetTrackballTime();
return down ? mWebView.onKeyDown(keyCode, event) : mWebView.onKeyUp(keyCode, event);
}
return false;
}
use of android.text.Spannable in project weex-example by KalicyZhou.
the class SearchBookContentsListItem method set.
public void set(SearchBookContentsResult result) {
pageNumberView.setText(result.getPageNumber());
String snippet = result.getSnippet();
if (snippet.isEmpty()) {
snippetView.setText("");
} else {
if (result.getValidSnippet()) {
String lowerQuery = SearchBookContentsResult.getQuery().toLowerCase(Locale.getDefault());
String lowerSnippet = snippet.toLowerCase(Locale.getDefault());
Spannable styledSnippet = new SpannableString(snippet);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
int queryLength = lowerQuery.length();
int offset = 0;
while (true) {
int pos = lowerSnippet.indexOf(lowerQuery, offset);
if (pos < 0) {
break;
}
styledSnippet.setSpan(boldSpan, pos, pos + queryLength, 0);
offset = pos + queryLength;
}
snippetView.setText(styledSnippet);
} else {
// This may be an error message, so don't try to bold the query terms within it
snippetView.setText(snippet);
}
}
}
use of android.text.Spannable in project android_frameworks_base by DirtyUnicorns.
the class FindActionModeCallback method setText.
/*
* 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;
}
use of android.text.Spannable in project android_frameworks_base by DirtyUnicorns.
the class BaseInputConnection method replaceText.
private void replaceText(CharSequence text, int newCursorPosition, boolean composing) {
final Editable content = getEditable();
if (content == null) {
return;
}
beginBatchEdit();
// delete composing text set previously.
int a = getComposingSpanStart(content);
int b = getComposingSpanEnd(content);
if (DEBUG)
Log.v(TAG, "Composing span: " + a + " to " + b);
if (b < a) {
int tmp = a;
a = b;
b = tmp;
}
if (a != -1 && b != -1) {
removeComposingSpans(content);
} else {
a = Selection.getSelectionStart(content);
b = Selection.getSelectionEnd(content);
if (a < 0)
a = 0;
if (b < 0)
b = 0;
if (b < a) {
int tmp = a;
a = b;
b = tmp;
}
}
if (composing) {
Spannable sp = null;
if (!(text instanceof Spannable)) {
sp = new SpannableStringBuilder(text);
text = sp;
ensureDefaultComposingSpans();
if (mDefaultComposingSpans != null) {
for (int i = 0; i < mDefaultComposingSpans.length; ++i) {
sp.setSpan(mDefaultComposingSpans[i], 0, sp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
}
}
} else {
sp = (Spannable) text;
}
setComposingSpans(sp);
}
if (DEBUG)
Log.v(TAG, "Replacing from " + a + " to " + b + " with \"" + text + "\", composing=" + composing + ", type=" + text.getClass().getCanonicalName());
if (DEBUG) {
LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG);
lp.println("Current text:");
TextUtils.dumpSpans(content, lp, " ");
lp.println("Composing text:");
TextUtils.dumpSpans(text, lp, " ");
}
// we are providing here.
if (newCursorPosition > 0) {
newCursorPosition += b - 1;
} else {
newCursorPosition += a;
}
if (newCursorPosition < 0)
newCursorPosition = 0;
if (newCursorPosition > content.length())
newCursorPosition = content.length();
Selection.setSelection(content, newCursorPosition);
content.replace(a, b, text);
if (DEBUG) {
LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG);
lp.println("Final text:");
TextUtils.dumpSpans(content, lp, " ");
}
endBatchEdit();
}
use of android.text.Spannable in project android_frameworks_base by DirtyUnicorns.
the class TextView method removeSuggestionSpans.
/**
* Removes the suggestion spans.
*/
CharSequence removeSuggestionSpans(CharSequence text) {
if (text instanceof Spanned) {
Spannable spannable;
if (text instanceof Spannable) {
spannable = (Spannable) text;
} else {
spannable = new SpannableString(text);
text = spannable;
}
SuggestionSpan[] spans = spannable.getSpans(0, text.length(), SuggestionSpan.class);
for (int i = 0; i < spans.length; i++) {
spannable.removeSpan(spans[i]);
}
}
return text;
}
Aggregations