当前位置:   article > 正文

EditText中onEditorAction监听事件执行两次

oneditoraction

调试开源项目android_serialport_api时,遇到onEditorAction执行两次的问题

更具参考文档解释:Android的EditText通过setOnEditorActionListener给文本编辑框设置监听事件,但是在其处理方法onEditorAction中的逻辑在每次回车后都触发了两次,原来是在键盘回车的ACTION_UP和ACTION_DOWN时都会触发这个方法,后面添加打印信息的时候和这个说法稍微有点出入,求大神解释

原始代码为

  1. Emission.setOnEditorActionListener(new OnEditorActionListener() {
  2. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  3. int i;
  4. CharSequence t = v.getText();
  5. char[] text = new char[t.length()];
  6. for (i=0; i<t.length(); i++) {
  7. text[i] = t.charAt(i);
  8. }
  9. try {
  10. mOutputStream.write(new String(text).getBytes());
  11. mOutputStream.write('\n');
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. return false;
  16. }
  17. });

添加打印信如下

  1. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  2. if (event == null)
  3. Log.d("chenzhen","chenzhen event null");
  4. if (event != null && event.getAction() == KeyEvent.ACTION_UP)
  5. Log.d("chenzhen","ACTION_UP");
  6. if (event != null && event.getAction() == KeyEvent.ACTION_DOWN)
  7. Log.d("chenzhen","ACTION_DOWN");
  8. if (actionId == EditorInfo.IME_ACTION_SEND)
  9. Log.d("chenzhen", "actionID is send");
  10. int i;
  11. CharSequence t = v.getText();
  12. char[] text = new char[t.length()];
  13. for (i=0; i<t.length(); i++) {
  14. text[i] = t.charAt(i);
  15. }
  16. Log.d("chenzhen", "chenzhen write");
  17. try {
  18. mOutputStream.write(new String(text).getBytes());
  19. mOutputStream.write('\n');
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. return false;
  24. }

打印结果为

01-01 03:08:12.585  2736  2736 D chenzhen: chenzhen event null
01-01 03:08:12.585  2736  2736 D chenzhen: actionID is send
01-01 03:08:12.585  2736  2736 D chenzhen: chenzhen write
01-01 03:08:12.625  2736  2736 D chenzhen: ACTION_DOWN
01-01 03:08:12.625  2736  2736 D chenzhen: chenzhen write

根据结果可以分析

第一次调用时  event 是空的,第二次调用时event不为空且getAction()为ACTION_DOWN

第一次调用时actionID是IME_ACTION_SEND,第二次调用时不为IME_ACTION_SEND


所以过滤的方法有两种

1 通过actionID进行过滤,因为只有第一次的actionID为IME_ACTION_SEND

  1. Emission.setOnEditorActionListener(new OnEditorActionListener() {
  2. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  3. if (actionId == EditorInfo.IME_ACTION_SEND ) {
  4. int i;
  5. CharSequence t = v.getText();
  6. char[] text = new char[t.length()];
  7. for (i=0; i<t.length(); i++) {
  8. text[i] = t.charAt(i);
  9. }
  10. try {
  11. mOutputStream.write(new String(text).getBytes());
  12. mOutputStream.write('\n');
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. return false;
  18. }
  19. });

2 通过event函数是否为空进行过滤,因为第一次event函数为空,所以修改如下

  1. Emission.setOnEditorActionListener(new OnEditorActionListener() {
  2. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  3. if (event == NULL ) {
  4. int i;
  5. CharSequence t = v.getText();
  6. char[] text = new char[t.length()];
  7. for (i=0; i<t.length(); i++) {
  8. text[i] = t.charAt(i);
  9. }
  10. try {
  11. mOutputStream.write(new String(text).getBytes());
  12. mOutputStream.write('\n');
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. return false;
  18. }
  19. });


参考文章:http://blog.csdn.net/u010825468/article/details/42874709

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/391094
推荐阅读
相关标签
  

闽ICP备14008679号