Android activity and view for the keyboard monitoring, mainly the following methods
// Press the button Public boolean onKeyDown (int keycode, keyevent event) {}// keys bounce public boolean onKeyUp (int keycode, KeyEvent event) {}// Constant Press Public boolean onkeylongpress (int keycode, keyevent event) {}
The first thing we look at today is to press and bounce two events, the transfer relationship between activity and view.
Let's look at the following test code
The first is the custom view, rewriting the onkeydown and onkeyup two methods
@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { //TODO auto-generated Method Stub BooleanA =Super. OnKeyDown (KeyCode, event); LOG.E ("123", "view_onkeydown|a===" +a); return a; } @Override Public BooleanOnKeyUp (intKeyCode, KeyEvent event) { //TODO auto-generated Method Stub BooleanA =Super. OnKeyUp (KeyCode, event); LOG.E ("123", "view_onkeyup|a==" +a); return a; }
The next step is to rewrite the above two methods in the activity
@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { //TODO auto-generated Method StubLOG.E ("123", "Activity_onkeydown"); return Super. OnKeyDown (KeyCode, event); } @Override Public BooleanOnKeyUp (intKeyCode, KeyEvent event) { //TODO auto-generated Method StubLOG.E ("123", "Activity_onkeyup"); return Super. OnKeyUp (KeyCode, event); }
Then we'll lay the log.
1. When the focus is not on the custom view
1 xx- on ,: -:54.755: e/123(9250): Activity_onkeydown2 on- on ,: -:54.951: e/123(9250): Activity_onkeyup3 on- on ,: -:55.757: e/123(9250): Activity_onkeydown4 on- on ,: -:55.964: e/123(9250): Activity_onkeyup
2. When the focus is on the view
1 on- on the: on:14.914: e/123(9250): view_onkeydown|a===true 2 on- on the: on:15.043: e/123(9250): view_onkeyup|a==false 3 on- on the: on:15.044: e/123(9250): Activity_onkeyup4 on- on the: on:16.451: e/123(9250): view_onkeydown|a===true 5 on- on the: on:16.614: e/123(9250): view_onkeyup|a==false 6 on- on the: on:16.615: e/123(9250): Activity_onkeyup
Summarize:
From the above log result, we can get the following two points of knowledge
1. The control listens to the keyboard, only when the control has the focus, can be effective, activity on the control of the monitoring is always there
2. If the control has the focus, the listener order is, first view, then activity
3. If the view returns to Ture, then the activity corresponding listener will not accept the key event
View-activity of the control on the OnKey event