Because of business needs, sometimes we listen to the soft keyboard down the action, when we press the down button, you can listen to perform the corresponding action.
So we write down the following code
@Override publicbooleanonKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK){ "onKeyDown"); } returnsuper.onKeyDown(keyCode, event); }
We press the down button, drip, no response. Press again, drop, hold the grass, exit directly, this time only log print out. This log is exited from the current Activity, not the soft keyboard downward.
If you don't think of a good solution, ask Google, and then we get the solution, which is to rewrite the Edittext.onkeypreime () method
/** * Intercept keyboard down Edittextview */ Public class textedittextview extends EditText { Public Textedittextview(Context context) {Super(context); } Public Textedittextview(context context, AttributeSet attrs) {Super(context, attrs); } Public Textedittextview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); }@Override Public Boolean Onkeypreime(intKeyCode, KeyEvent event) {if(KeyCode = = Keyevent.keycode_back && event.getaction () = =1) {LOG.I ("Main_activity","Keyboard Down");Super. Onkeypreime (KeyCode, event);return false; }return Super. Onkeypreime (KeyCode, event); }}
In the layout
<?xml version= "1.0" encoding= "Utf-8"?><relativelayout android: ID = "@+id/activity_main" xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:background =" @android: Color/white "; <com.yxhuang.myapplication.TextEditTextViewandroid:id="@+id/edt_text" Android:layout_width="Match_parent"android:hint="Test"android:layout_ MarginTop="30DP"android:textcolorhint="@android: Color/black"android: Layout_height="wrap_content"/> </relativelayout>
When you click Down, you can see the log
This is the keyboard down listening, we get it, then we can perform the corresponding action.
As a programmer, we need to know not only what to do but also why. This issue involves Android input system, see the previous blog post for more information on Android input system
Why is rewriting the Activity.onkeydown () method useless?
Based on the Android input system article, we know the sequence of pipeline processing of events
IME is the keyboard input method, Viewpreimestage is before the input method, Imestage is the Input method processing, Viewpostimestage is ultimately passed to Activity.onkeydown ().
Viewpreimestage can pass keyboard-down events to View, but not to viewpostimestage, which should be something that consumes this event. When we press the down button, the keyboard will be put away, apparently the keyboard consumes this event.
Let's see Imestage's source code.
/** * Delivers input events to the IME. * Does not support pointer events. */ Final class imeinputstage extends asyncinputstage implements Inputmethodmanager. Finishedinputeventcallback { PublicImeinputstage (inputstage Next, String tracecounter) {Super(Next, Tracecounter); } @Overrideprotected intOnprocess (queuedinputevent q) {if(Mlastwasimtarget) {//Get Inputmethodmanager instancesInputmethodmanager IMM = Inputmethodmanager.peekinstance ();if(IMM! =NULL) {FinalInputEvent event = q.mevent;the//Dispatchinputevent method handles the event intresult = Imm.dispatchinputevent (event, Q, This, Mhandler);if(Result = = inputmethodmanager.dispatch_handled) {returnfinish_handled; }Else if(Result = = inputmethodmanager.dispatch_not_handled) {returnfinish_not_handled; }Else{returnDEFER;//Callback'll be invoked later} } }returnFORWARD; } @Override Public voidOnfinishedinputevent (Object token,BooleanHandled) {Queuedinputevent q = (queuedinputevent) token;if(handled) {Finish (q,true);return; } forward (q); } }
We see that if the Inputmethodemanager.dispatchinputevent (...) method returns the result is
Inputmethodmanager.dispatch_handled the delivery of the end event. The event is not delivered to viewpostimestage, and thus cannot be passed to Activity.onkeydown ().
Come here we finally know why rewriting the Activity.onkeydown () method is useless, want to listen to the keyboard down the event, you need to rewrite the Edittext.onkeypreime () method, in this method to listen.
Listening to the Android soft keyboard down