赞
踩
调试开源项目android_serialport_api时,遇到onEditorAction执行两次的问题
更具参考文档解释:Android的EditText通过setOnEditorActionListener给文本编辑框设置监听事件,但是在其处理方法onEditorAction中的逻辑在每次回车后都触发了两次,原来是在键盘回车的ACTION_UP和ACTION_DOWN时都会触发这个方法,后面添加打印信息的时候和这个说法稍微有点出入,求大神解释
原始代码为
- Emission.setOnEditorActionListener(new OnEditorActionListener() {
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- int i;
- CharSequence t = v.getText();
- char[] text = new char[t.length()];
- for (i=0; i<t.length(); i++) {
- text[i] = t.charAt(i);
- }
- try {
- mOutputStream.write(new String(text).getBytes());
- mOutputStream.write('\n');
- } catch (IOException e) {
- e.printStackTrace();
- }
- return false;
- }
- });
添加打印信如下
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- if (event == null)
- Log.d("chenzhen","chenzhen event null");
- if (event != null && event.getAction() == KeyEvent.ACTION_UP)
- Log.d("chenzhen","ACTION_UP");
- if (event != null && event.getAction() == KeyEvent.ACTION_DOWN)
- Log.d("chenzhen","ACTION_DOWN");
-
- if (actionId == EditorInfo.IME_ACTION_SEND)
- Log.d("chenzhen", "actionID is send");
- int i;
- CharSequence t = v.getText();
- char[] text = new char[t.length()];
- for (i=0; i<t.length(); i++) {
- text[i] = t.charAt(i);
- }
- Log.d("chenzhen", "chenzhen write");
- try {
- mOutputStream.write(new String(text).getBytes());
- mOutputStream.write('\n');
- } catch (IOException e) {
- e.printStackTrace();
- }
- return false;
- }
打印结果为
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
- Emission.setOnEditorActionListener(new OnEditorActionListener() {
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- if (actionId == EditorInfo.IME_ACTION_SEND ) {
- int i;
- CharSequence t = v.getText();
- char[] text = new char[t.length()];
- for (i=0; i<t.length(); i++) {
- text[i] = t.charAt(i);
- }
- try {
- mOutputStream.write(new String(text).getBytes());
- mOutputStream.write('\n');
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return false;
- }
- });
2 通过event函数是否为空进行过滤,因为第一次event函数为空,所以修改如下
- Emission.setOnEditorActionListener(new OnEditorActionListener() {
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- if (event == NULL ) {
- int i;
- CharSequence t = v.getText();
- char[] text = new char[t.length()];
- for (i=0; i<t.length(); i++) {
- text[i] = t.charAt(i);
- }
- try {
- mOutputStream.write(new String(text).getBytes());
- mOutputStream.write('\n');
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return false;
- }
- });
参考文章:http://blog.csdn.net/u010825468/article/details/42874709
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。