android EditText或AutoCompleteTextView setOnKey事件问题

今天 修改项目想把首页搜索改下,当用户输入完关键字按下软件键盘回车后跳转结果页

代码没问题不知道为什么老是跳转二次,Debug调试发现setOnKeyListener执行二次一直未找到解决办希望有遇到这问题的人指教下

最后查了下中文版的API发现有一个setOnEditorActionListener事件

我把搜索事件setOnKeyListener改为setOnEditorActionListener事件

然后把XML改为 

<EditText
android:id="@+id/searbox"
style="@style/indexsearchbox"
android:layout_toLeftOf="@id/searchbtn"
android:hint="@string/pleasekey" 
android:layout_height="45dip"
android:singleLine="true"
android:imeOptions="actionGo"/>这个意思是将软件键盘上的回车键改为GO

  

代码:

//单击软件键盘回车事件

//单击软件键盘回车事件
	    txtkeyword.setOnEditorActionListener(new EditText.OnEditorActionListener() { 
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				// TODO Auto-generated method stub
				if(actionId ==EditorInfo.IME_ACTION_GO){
					 //跳转activity
					return true;
					}else{
						Toast.makeText(context, R.string.key_word, Toast.LENGTH_SHORT).show(); 

						return true;
					}
				}  
				return false;
			}
		});

  

在测试与调试没问题成功。

就这一个跳转二次问题折腾几个小时 哎。。。。

原文地址:https://www.cnblogs.com/freexiaoyu/p/2425439.html