Android monitors soft keyboard return events

Keywords: Android

stay Android In the process of development, it is inevitable to encounter some "accidents". For example, the input method of soft keyboard monitoring problem, because the third-party input method is different (some key events have made some special processing), so sometimes some code will "fail". Assuming a scenario, EditText listens for the return event and sends the input content after the return. Generally, there are four ways to handle it:

Assume scenario diagram:


1.setImeOptions

Set with code:

mEditText.setImeOptions(EditorInfo.IME_ACTION_SEND);  

Or set it in the layout file:

    <EditText   
        ...  
            android:imeOptions="actionSend"  
            />  

2.setOnKeyListener

mEditText.setOnKeyListener(new View.OnKeyListener() {  
              
            @Override  
            public boolean onKey(View v, int keyCode, KeyEvent event) {  
                //Attention should be paid to judgment processing. ActionDown and ActionUp will be called back here. If they are not processed, they will be called twice.  
                if (KeyEvent.KEYCODE_ENTER == keyCode && KeyEvent.ACTION_DOWN == event.getAction()) {  
                    //Handling events  
                    return true;  
                }  
                return false;  
            }  
        }); 

However, this interface may "fail", see the documentation:

Register a callback to be invoked when a hardware key is pressed in this view. Key presses in software input methods will generally not trigger the methods of this listener. 
3.dispatchKeyEvent

You can also overwrite dispatchKey Event in Activity or Dialog to achieve your goal:

    @Override  
        public boolean dispatchKeyEvent(KeyEvent event) {  
            //Attention should be paid to judgment processing. ActionDown and ActionUp will be called back here. If they are not processed, they will be called twice.  
            if (KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction()) {  
                //Handling events  
                return true;  
            }  
            return super.dispatchKeyEvent(event);  
        }  

4.setOnEditorActionListener

This method should be the most orthodox and appropriate, general practice:

    mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
                  
                @Override  
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
                    if (actionId == EditorInfo.IME_ACTION_SEND) {  
                        //Handling events  
                    }  
                    return false;  
                }  
            });  

Of course, this premise is to use the method in 1 to set up ImeOption, that is, to change the action of the return key on the soft keyboard. However, in practical use, it is found that different input methods have different effects. Some input methods will not change the action of the return key at all, that is, actionId!= EditorInfo.IME_ACTION_SEND, but=== EditorInfo.IME_ACTION_DONE. Often encounter such problems, the first reaction may be to choose 2, 3 methods, but 2, 3 methods may also be ineffective. How to adapt the soft keyboard? Looking back at onEditorAction, we find that this callback interface has three parameters. Looking at methods 1, 2 and 3, we can see that IME_ACTION_XX and KeyEvent objects are used, and onEditorAction callback parameters have two. We just need to do a little more in the onEditor Action. For example:
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
              
            @Override  
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
                //Triggered when actionId = XX_SEND or XX_DONE  
                //Or event. getKeyCode = ENTER and event. getAction = ACTION_DOWN also triggers  
                //Note that this is a must to judge event!= null. Because null is returned in some input methods.  
                if (actionId == EditorInfo.IME_ACTION_SEND  
                        || actionId == EditorInfo.IME_ACTION_DONE  
                        || (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {  
                    //Handling events  
                }  
                return false;  
            }  
        }); 
Document description:
Set a special listener to be called when an action is performed on the text view. This will be called when the enter key is pressed, or when an action supplied to the IME is selected by the user. Setting this means that the normal hard key event will not insert a newline into the text view, even if it is multi-line; holding down the ALT modifier will, however, allow the user to insert a newline character.


Summary: Soft keyboard return key events can be easily solved with method 4, which is also the most appropriate method.

I hope it will be helpful to you. Thank you.


—— Detailed Essay on Android, April 24, 2014

http://blog.csdn.net/Iceshow0428/article/details/24428417

Posted by Fusioned on Wed, 10 Jul 2019 17:21:52 -0700