When searching for a bug in the android browser over the past few days, I found that this function has stopped a classic one. Now I want to record it.
Here, we use the input method to input "FF" in the textdialog view on the www.google.com webpage as an example. First, assume that "FF" has been entered ".
In order to enter the "younger brother", the user times the D character twice on the soft keyboard of the input method, and then the "DD" will be considered as "younger brother ", then, in the input method alternative box, select the desired "younger brother ". When "DD" reaches "younger brother", the following function is called:
Ontextchanged (charsequence S, int start, int before, int count)
// The parameter S is "Brother FF" before is 2, indicating that two characters ("DD") have been deleted and the Count value is 2, indicating that there are two characters ("brother ") added.
// The meanings of before and count are annotated in the // ontextchanged function of textview (cupcake/frameworks/base/CORE/Java/Android/widget/textview. Java.
Below, we will list the function code and add some comments.
Cupcake/frameworks/base/CORE/Java/Android/WebKit/textdialog. Java:
Protected void ontextchanged (charsequence S, int start, int before, int count ){
Super. ontextchanged (S, start, before, count );
String postchange = S. tostring (); // postchange is "Brother FF"
// Prevent callto settext from invoking ontextchanged (since this will
// Mean we are on a different textfield). Also Prevent the change when
// Going from a textfield with a string of text to one with a smaller
// Limit on text length from registering the ontextchanged event.
If (mprechange = NULL | mprechange. Equals (postchange) |
(Mmaxlength>-1 & mprechange. Length ()> mmaxlength &&
Mprechange. substring (0, mmaxlength). Equals (postchange ))){
Return;
}
Mprechange = postchange; // The value before mprechange is "ddff"
// This was simply a delete or a cut, so just delete
// Selection.
// This code is not executed, and it is used to delete and cut. Note: There is a data structure render tree in Android browser.
// Indicates the data. We need to update it, and there is also a cachedtext to indicate the data in textdialog. Both must be updated.
// The mwebview. deleteselection (start, start + before) code updates the underlying data such as the render tree.
// Updatecachedtextfield (); the Code updates the cached text.
If (before> 0 & 0 = count ){
Mwebview. deleteselection (start, start + before );
// For this and all changes to the text, update our cache
Updatecachedtextfield ();
Return;
}
// Find the last character being replaced. If it can be represented
// Events, we will pass them to native (after replacing the beginning
// Of the changed text), so we can see JavaScript events.
// Otherwise, replace the text being changed (including the last
// Character) in the textfield.
// This annotation is very good. The last character "" in "" won't get the keyevent from kmap, so (cannotusekeyevents = 0)
// Call mwebview. replacetextfieldtext () (update the string of the node that represents textdialog in the lower layer of browser) to set "DD"
// Replace it with "DD", and updatecachedtextfield () is called to update cachedtext. Because "" cannot be mapped to a keyevent,
// Therefore, senddomevent (events [I]) will not be called, and no event is sent to the Javascript section of WebKit.
Textutils. getchars (S, start + count-1, start + count, mcharacter, 0 );
Keycharactermap kmap =
Keycharactermap. Load (keycharactermap. built_in_keyboard );
Keyevent [] events = kmap. getevents (mcharacter );
Boolean cannotusekeyevents = NULL = events;
Int charactersfromkeyevents = cannotusekeyevents? 0: 1;
If (count> 1 | cannotusekeyevents ){
String replace = S. Subsequence (start,
Start + count-charactersfromkeyevents). tostring ();
Mwebview. replacetextfieldtext (start, start + before, replace,
Start + count-charactersfromkeyevents,
Start + count-charactersfromkeyevents); // Replace "DD" with "younger brother" at the bottom layer of browser"
} Else {
// This corrects the selection which may have been affected by
// Trackball or auto-correct.
Mwebview. setselection (start, start + before );
}
Updatecachedtextfield (); // Replace "DD" in cached text with "younger brother"
If (cannotusekeyevents ){
Return;
}
Int length = events. length;
For (INT I = 0; I <length; I ++ ){
// We never send modifier keys to native code so don't send them
// Here either.
If (! Keyevent. ismodifierkey (events [I]. getkeycode ())){
Senddomevent (events [I]);
}
}
}