diff options
| author | 2017-03-31 16:37:01 +0000 | |
|---|---|---|
| committer | 2017-03-31 16:37:01 +0000 | |
| commit | ef1ba7b10b684e63f990c9f20a015a9658a3d4be (patch) | |
| tree | 512a7dc41a492a3a0b400df3a20942c815b1f178 | |
| parent | 4cef691344d18db0701a51bcfa0cf44c314515d5 (diff) | |
| parent | 523faba79aac364f740d38b2f90f11d94f431fd7 (diff) | |
Merge "Prevent arrow keys from leaving editable TextViews" into oc-dev am: 27b0a55fab
am: 523faba79a
Change-Id: Icfcc66ae6bd344464cc67f4db183a814e2644ce8
| -rw-r--r-- | core/java/android/widget/TextView.java | 42 |
1 files changed, 16 insertions, 26 deletions
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 3d5e81bcae33..6f8df36b7bf5 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -367,14 +367,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private TextClassifier mTextClassifier; - // A flag to prevent repeated movements from escaping the enclosing text view. The idea here is - // that if a user is holding down a movement key to traverse text, we shouldn't also traverse - // the view hierarchy. On the other hand, if the user is using the movement key to traverse - // views (i.e. the first movement was to traverse out of this view, or this view was traversed - // into by the user holding the movement key down) then we shouldn't prevent the focus from - // changing. - private boolean mPreventDefaultMovement; - private TextUtils.TruncateAt mEllipsize; static class Drawables { @@ -6982,20 +6974,22 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener return true; } + private boolean isDirectionalNavigationKey(int keyCode) { + switch(keyCode) { + case KeyEvent.KEYCODE_DPAD_UP: + case KeyEvent.KEYCODE_DPAD_DOWN: + case KeyEvent.KEYCODE_DPAD_LEFT: + case KeyEvent.KEYCODE_DPAD_RIGHT: + return true; + } + return false; + } + private int doKeyDown(int keyCode, KeyEvent event, KeyEvent otherEvent) { if (!isEnabled()) { return KEY_EVENT_NOT_HANDLED; } - // If this is the initial keydown, we don't want to prevent a movement away from this view. - // While this shouldn't be necessary because any time we're preventing default movement we - // should be restricting the focus to remain within this view, thus we'll also receive - // the key up event, occasionally key up events will get dropped and we don't want to - // prevent the user from traversing out of this on the next key down. - if (event.getRepeatCount() == 0 && !KeyEvent.isModifierKey(keyCode)) { - mPreventDefaultMovement = false; - } - switch (keyCode) { case KeyEvent.KEYCODE_ENTER: if (event.hasNoModifiers()) { @@ -7127,16 +7121,16 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } if (doDown) { if (mMovement.onKeyDown(this, (Spannable) mText, keyCode, event)) { - if (event.getRepeatCount() == 0 && !KeyEvent.isModifierKey(keyCode)) { - mPreventDefaultMovement = true; - } return KEY_DOWN_HANDLED_BY_MOVEMENT_METHOD; } } + // Consume arrows to prevent focus leaving the editor. + if (isDirectionalNavigationKey(keyCode)) { + return KEY_EVENT_HANDLED; + } } - return mPreventDefaultMovement && !KeyEvent.isModifierKey(keyCode) - ? KEY_EVENT_HANDLED : KEY_EVENT_NOT_HANDLED; + return KEY_EVENT_NOT_HANDLED; } /** @@ -7169,10 +7163,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener return super.onKeyUp(keyCode, event); } - if (!KeyEvent.isModifierKey(keyCode)) { - mPreventDefaultMovement = false; - } - switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: if (event.hasNoModifiers()) { |