@@ -607,23 +607,31 @@ protected void onTextChanged(CharSequence s,int start,int before,int count){
607607 // character or the existing selection, so it will not get cleared
608608 // above.
609609 mGotDelete = false ;
610+ // Prefer sending javascript events, so when adding one character,
611+ // don't replace the unchanged text.
612+ if (count > 1 && before == count - 1 ) {
613+ String replaceButOne = s .subSequence (start ,
614+ start + before ).toString ();
615+ String replacedString = getText ().subSequence (start ,
616+ start + before ).toString ();
617+ if (replaceButOne .equals (replacedString )) {
618+ // we're just adding one character
619+ start += before ;
620+ before = 0 ;
621+ count = 1 ;
622+ }
623+ }
610624 // Find the last character being replaced. If it can be represented by
611- // events, we will pass them to native (after replacing the beginning
612- // of the changed text), so we can see javascript events.
613- // Otherwise, replace the text being changed (including the last
614- // character) in the textfield.
615- TextUtils .getChars (s , start + count - 1 , start + count , mCharacter , 0 );
616- KeyCharacterMap kmap = KeyCharacterMap .load (KeyCharacterMap .VIRTUAL_KEYBOARD );
617- KeyEvent [] events = kmap .getEvents (mCharacter );
618- boolean cannotUseKeyEvents = null == events ;
619- int charactersFromKeyEvents = cannotUseKeyEvents ? 0 : 1 ;
620- if (count > 1 || cannotUseKeyEvents ) {
621- String replace = s .subSequence (start ,
622- start + count - charactersFromKeyEvents ).toString ();
623- mWebView .replaceTextfieldText (start , start + before , replace ,
624- start + count - charactersFromKeyEvents ,
625- start + count - charactersFromKeyEvents );
626- } else {
625+ // events, we will pass them to native so we can see javascript events.
626+ // Otherwise, replace the text being changed in the textfield.
627+ KeyEvent [] events = null ;
628+ if (count == 1 ) {
629+ TextUtils .getChars (s , start + count - 1 , start + count , mCharacter , 0 );
630+ KeyCharacterMap kmap = KeyCharacterMap .load (KeyCharacterMap .VIRTUAL_KEYBOARD );
631+ events = kmap .getEvents (mCharacter );
632+ }
633+ boolean useKeyEvents = (events != null );
634+ if (useKeyEvents ) {
627635 // This corrects the selection which may have been affected by the
628636 // trackball or auto-correct.
629637 if (DebugFlags .WEB_TEXT_VIEW ) {
@@ -633,8 +641,6 @@ protected void onTextChanged(CharSequence s,int start,int before,int count){
633641 if (!mInSetTextAndKeepSelection ) {
634642 mWebView .setSelection (start , start + before );
635643 }
636- }
637- if (!cannotUseKeyEvents ) {
638644 int length = events .length ;
639645 for (int i = 0 ; i < length ; i ++) {
640646 // We never send modifier keys to native code so don't send them
@@ -643,6 +649,12 @@ protected void onTextChanged(CharSequence s,int start,int before,int count){
643649 sendDomEvent (events [i ]);
644650 }
645651 }
652+ } else {
653+ String replace = s .subSequence (start ,
654+ start + count ).toString ();
655+ mWebView .replaceTextfieldText (start , start + before , replace ,
656+ start + count ,
657+ start + count );
646658 }
647659 updateCachedTextfield ();
648660 }
@@ -966,8 +978,11 @@ private void setMaxLength(int maxLength) {
966978 * @param text The new text to place in the textfield.
967979 */
968980 /* package */ void setTextAndKeepSelection (String text ) {
969- mPreChange = text .toString ();
970981 Editable edit = getText ();
982+ mPreChange = text ;
983+ if (edit .toString ().equals (text )) {
984+ return ;
985+ }
971986 int selStart = Selection .getSelectionStart (edit );
972987 int selEnd = Selection .getSelectionEnd (edit );
973988 mInSetTextAndKeepSelection = true ;
@@ -1075,6 +1090,7 @@ private void setMaxLength(int maxLength) {
10751090 setMaxLength (maxLength );
10761091 setHorizontallyScrolling (single );
10771092 setInputType (inputType );
1093+ clearComposingText ();
10781094 setImeOptions (imeOptions );
10791095 setVisibility (VISIBLE );
10801096 if (!autoComplete ) {
0 commit comments