Android监听软键盘回车事件

来源:互联网 发布:一个女人的史诗 知乎 编辑:程序博客网 时间:2024/05/20 18:52

在Android开发中,难免会碰到一些”意外“。比如输入法软按键监听问题,因为第三方输入法各有不同(对一些按键事件作了一些特殊的处理),所以有时有些代码会“失灵”。假设一个场景,EditText监听回车事件,回车后就发送输入的内容,一般有以下4种处理方式:

假设场景图:


1.setImeOptions

用代码设置:

[java] view plain copy
 print?
  1. mEditText.setImeOptions(EditorInfo.IME_ACTION_SEND);  
或者在布局文件中设置:

[html] view plain copy
 print?
  1. <EditText   
  2.     ...  
  3.         android:imeOptions="actionSend"  
  4.         />  

2.setOnKeyListener

[java] view plain copy
 print?
  1. mEditText.setOnKeyListener(new View.OnKeyListener() {  
  2.               
  3.             @Override  
  4.             public boolean onKey(View v, int keyCode, KeyEvent event) {  
  5.                 //这里注意要作判断处理,ActionDown、ActionUp都会回调到这里,不作处理的话就会调用两次  
  6.                 if (KeyEvent.KEYCODE_ENTER == keyCode && KeyEvent.ACTION_DOWN == event.getAction()) {  
  7.                     //处理事件  
  8.                     return true;  
  9.                 }  
  10.                 return false;  
  11.             }  
  12.         });  
但是,这个接口有可能会“失灵”,看说明文档:

[plain] view plain copy
 print?
  1. 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

也可以在Activity或者Dialog里覆写dispatchKeyEvent来达到目的:

[java] view plain copy
 print?
  1. @Override  
  2.     public boolean dispatchKeyEvent(KeyEvent event) {  
  3.         //这里注意要作判断处理,ActionDown、ActionUp都会回调到这里,不作处理的话就会调用两次  
  4.         if (KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction()) {  
  5.             //处理事件  
  6.             return true;  
  7.         }  
  8.         return super.dispatchKeyEvent(event);  
  9.     }  

4.setOnEditorActionListener

这个方法应该是最正统也最合适的,一般的做法:

[java] view plain copy
 print?
  1. mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
  2.               
  3.             @Override  
  4.             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  5.                 if (actionId == EditorInfo.IME_ACTION_SEND) {  
  6.                     //处理事件  
  7.                 }  
  8.                 return false;  
  9.             }  
  10.         });  
当然这个前提是要用1中的方法设置ImeOption,也就是改变软键盘上回车键的Action。但是,在实际使用中,发现在不同输入法上效果不同,某些输入法上根本不会改变回车键的Action,也就是actionId != EditorInfo.IME_ACTION_SEND,而是==EditorInfo.IME_ACTION_DONE。往往遇到如此问题时,可能第一反应就是选择2、3方法,但是2、3方法也有可能不奏效。那该怎么去适配软键盘呢?目光回到onEditorAction上,发现此回调接口带有3个参数,再看1、2、3方法,其实用到的就是IME_ACTION_XX和KeyEvent对象,而onEditorAction回调参数就有这两个。我们只需要在onEditorAction里再稍作处理就行了。比如:

[java] view plain copy
 print?
  1. mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
  2.               
  3.             @Override  
  4.             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  5.                 //当actionId == XX_SEND 或者 XX_DONE时都触发  
  6.                 //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发  
  7.                 //注意,这是一定要判断event != null。因为在某些输入法上会返回null。  
  8.                 if (actionId == EditorInfo.IME_ACTION_SEND  
  9.                         || actionId == EditorInfo.IME_ACTION_DONE  
  10.                         || (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {  
  11.                     //处理事件  
  12.                 }  
  13.                 return false;  
  14.             }  
  15.         });  

文档说明:

[plain] view plain copy
 print?
  1. 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.  

总结:软键盘回车键事件只需要用方法4就可以轻松解决,也是最合适的方法。

下面是原创:

注意清单文件中,千万不要写:

 

android:windowSoftInputMode="stateHidden  因为写了这个在调用隐藏软键盘的时候会造成
getWindow().getAttributes().softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
永远不会进入到
protected void hideSoftKeyboard() {    if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {        if (getCurrentFocus() != null)            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),                    InputMethodManager.HIDE_NOT_ALWAYS);    }}
执行隐藏软键盘的方法!!!

原创粉丝点击