diff options
| author | The Android Open Source Project <initial-contribution@android.com> | 2009-03-19 23:08:54 -0700 |
|---|---|---|
| committer | The Android Open Source Project <initial-contribution@android.com> | 2009-03-19 23:08:54 -0700 |
| commit | c2ad241504fcaa12d4579d3b0b4038d1ca8d08c9 (patch) | |
| tree | 1a260a5b0b371678c9a4710ea36030db14374e56 /core/java/android/widget/TextView.java | |
| parent | 105925376f8d0f6b318c9938c7b83ef7fef094da (diff) | |
auto import from //branches/cupcake_rel/...@141571
Diffstat (limited to 'core/java/android/widget/TextView.java')
| -rw-r--r-- | core/java/android/widget/TextView.java | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 136752b9f3a2..81516b9158dc 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -727,7 +727,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE); } else if (phone) { mInput = DialerKeyListener.getInstance(); - inputType = EditorInfo.TYPE_CLASS_PHONE; + mInputType = inputType = EditorInfo.TYPE_CLASS_PHONE; } else if (numeric != 0) { mInput = DigitsKeyListener.getInstance((numeric & SIGNED) != 0, (numeric & DECIMAL) != 0); @@ -5421,6 +5421,62 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener return changed; } + /** + * Move the cursor, if needed, so that it is at an offset that is visible + * to the user. This will not move the cursor if it represents more than + * one character (a selection range). This will only work if the + * TextView contains spannable text; otherwise it will do nothing. + */ + public boolean moveCursorToVisibleOffset() { + if (!(mText instanceof Spannable)) { + return false; + } + int start = Selection.getSelectionStart(mText); + int end = Selection.getSelectionEnd(mText); + if (start != end) { + return false; + } + + // First: make sure the line is visible on screen: + + int line = mLayout.getLineForOffset(start); + + final int top = mLayout.getLineTop(line); + final int bottom = mLayout.getLineTop(line+1); + final int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom(); + int vslack = (bottom - top) / 2; + if (vslack > vspace / 4) + vslack = vspace / 4; + final int vs = mScrollY; + + if (top < (vs+vslack)) { + line = mLayout.getLineForVertical(vs+vslack+(bottom-top)); + } else if (bottom > (vspace+vs-vslack)) { + line = mLayout.getLineForVertical(vspace+vs-vslack-(bottom-top)); + } + + // Next: make sure the character is visible on screen: + + final int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(); + final int hs = mScrollX; + final int leftChar = mLayout.getOffsetForHorizontal(line, hs); + final int rightChar = mLayout.getOffsetForHorizontal(line, hspace+hs); + + int newStart = start; + if (newStart < leftChar) { + newStart = leftChar; + } else if (newStart > rightChar) { + newStart = rightChar; + } + + if (newStart != start) { + Selection.setSelection((Spannable)mText, newStart); + return true; + } + + return false; + } + @Override public void computeScroll() { if (mScroller != null) { |
