Android监听软键盘回车事件

来源:互联网 发布:embedpano.js 编辑:程序博客网 时间:2024/06/06 02:48
在Android开发中,难免会碰到一些”意外“。比如输入法软按键监听问题,因为第三方输入法各有不同(对一些按键事件作了一些特殊的处理),所以有时有些代码会“失灵”。假设一个场景,EditText监听回车事件,回车后就发送输入的内容,一般有以下4种处理方式:

假设场景图:


1.setImeOptions

用代码设置:

mEditText.setImeOptions(EditorInfo.IME_ACTION_SEND);  

或者在布局文件中设置:

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

2.setOnKeyListener

mEditText.setOnKeyListener(new View.OnKeyListener() {                            @Override              public boolean onKey(View v, int keyCode, KeyEvent event) {                  //这里注意要作判断处理,ActionDown、ActionUp都会回调到这里,不作处理的话就会调用两次                  if (KeyEvent.KEYCODE_ENTER == keyCode && KeyEvent.ACTION_DOWN == event.getAction()) {                      //处理事件                      return true;                  }                  return false;              }          }); 

但是,这个接口有可能会“失灵”,看说明文档:

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来达到目的:

    @Override          public boolean dispatchKeyEvent(KeyEvent event) {              //这里注意要作判断处理,ActionDown、ActionUp都会回调到这里,不作处理的话就会调用两次              if (KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction()) {                  //处理事件                  return true;              }              return super.dispatchKeyEvent(event);          }  

4.setOnEditorActionListener

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

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

当然这个前提是要用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里再稍作处理就行了。比如:
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {                            @Override              public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {                  //当actionId == XX_SEND 或者 XX_DONE时都触发                  //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发                  //注意,这是一定要判断event != null。因为在某些输入法上会返回null。                  if (actionId == EditorInfo.IME_ACTION_SEND                          || actionId == EditorInfo.IME_ACTION_DONE                          || (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {                      //处理事件                  }                  return false;              }          }); 
文档说明:
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细节随笔》,于2014.4.24

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

0 0