EditText input Key 1, what happened to the system?

Source: Internet
Author: User

The

button triggers a trigger message to be sent to MessageQueue with the following message:
{when=-20s330ms what=11 obj=keyevent {action=action_down, Keycode=keycode_ 1, scancode=0, metastate=0, flags=0x6, repeatcount=0, eventtime=3939450, downtime=3939450, DeviceId=-1, source=0x0} Target=android.view.viewrootimpl$viewroothandler}
This message is processed in Viewrootimpl$viewroothandler, and the corresponding Hanlemessage section is
@Override
public void Handlemessage (Message msg) {
Switch (msg.what) {
Case msg_dispatch_key_from_ime: { br> if (LOCAL_LOGV) log.v (
TAG, "dispatching key"
+ Msg.obj + "from IME to" + MView);
KeyEvent event = (keyevent) msg.obj;
If ((Event.getflags () &keyevent.flag_from_system)! = 0) {
//The IME is trying to say this event was from the//system! Bad Bad bad!
//noinspection unusedassignment
event = Keyevent.changeflags (event, Event.getflags () &
~keyevent.flag _from_system);
}
Enqueueinputevent (event, NULL, Queuedinputevent.flag_deliver_post_ime, True);
} break;

Jump several times in Viewrootimpl to enter the Processkeyevent method
private int processkeyevent (Queuedinputevent q) {
Final KeyEvent event = (keyevent) q.mevent;

Deliver the key to the view hierarchy.
The Dispatchkeyevent method of the view is called, where Mview is Phonewindow$decorview
if (Mview.dispatchkeyevent (event)) {
return finish_handled;
}
//.....
return FORWARD;
}

Starting from Decorview a number of dispatchkeyevent (event), KeyEvent is eventually distributed to TextView TextView () method that executes onkeydown:
@Override
public boolean onKeyDown (int keycode, keyevent event) {
int which = Dokeydown (keycode, event, NULL);
if (which = = 0) {
Return Super.onkeydown (KeyCode, event);
}

return true;
}
TextView's Dokeydown () was executed at the beginning of the onkeydown () method
public boolean onKeyDown (int keycode, keyevent event) {
int which = Dokeydown (keycode, event, NULL);
if (which = = 0) {
Return Super.onkeydown (KeyCode, event);
}

Return true;
}
The first line of the function executes TextView's Dokeydown () method, which is tracked in
private int dokeydown (int keycode, keyevent event, KeyEvent otherevent) "
if (meditor! = NULL && Meditor.mkeylistener! = null) {
if (dodown) {
BeginBatchEdit ();
Final Boolean handled = MEditor.mKeyListener.onKeyDown (this, (Editable) MText,
KeyCode, event);
Endbatchedit ();
Hideerrorifunchanged ();
if (handled) return 1;
}
}
}
This method executes the Mkeylisterner.onkeydown () method:
@Override
Public boolean onKeyDown (view view, Editable content,
int keycode, keyevent event) {
KeyListener im = Getkeylistener (event);

Return Im.onkeydown (view, content, KeyCode, event);
}
The keyevent corresponding KeyListener is judged by Getkeylistener (event), and then the corresponding onkeydown is executed.
For key 1, the KeyListener for Qwertykeylistener,qwertykeylistener.onkeydown () is:
public Boolean OnKeyDown (view view, Editable content,
int KeyCode, KeyEvent event) {
//...
Content.replace (SelStart, Selend, string.valueof (char) i);
//...
}
This calls the Replace method of Spannablestringbuilder:
Public Spannablestringbuilder replace (final int start, final int end,
Charsequence TB, int tbstart, int tbend) {
//...
textwatcher[] Textwatchers = Getspans (start, start + Origlen, textwatcher.class);
Sendbeforetextchanged (Textwatchers, start, Origlen, Newlen);

Change (start, end, TB, Tbstart, tbend);

//...
Sendtextchanged (Textwatchers, start, Origlen, Newlen);
Sendaftertextchanged (textwatchers);

Span watchers need to being called after text watchers, which may update the layout
Sendtospanwatchers (Start, end, Newlen-origlen);

return this;
}

This method obtains the TextView all the Textwatcher, then sends the beforetextchanged message, and then calls the change () method to really
Changed the content in the TextView. Then send the OnTextChanged and Aftertextchanged methods.


Such a process can be simply summed up in such a few steps:
1. Press the keyboard and the system sends a KEYDOWN message to Mainlooper's MessageQueue.
2.viewrootimpl$viewroothandler processing KeyDown messages, calling Decorview's dispatchkeyevent for distribution
3.DocoreView distributes the KeyEvent to the Dokeydown () method of TextView execution TextView.
4.TextView calls the Mkeylistener.onkeydown () method.
5. Finally call the onkeydown () method of the appropriate KeyListener (here, Qwertykeylistener) to change the Mtext value in TextView

EditText input Key 1, what happened to the system?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.