EditText中onEditorAction监听事件执行两次

调试开源项目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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值