码迷,mamicode.com
首页 > 移动开发 > 详细

android设置软键盘搜索键以及监听搜索键点击时发生两次事件的问题解决

时间:2016-04-03 15:48:03      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

在输入框中加入android:imeOptions="actionSearch",调用软键盘时,回车键就会显示搜索二字。

我想在点击搜索时,跳转到下一个页面,但是调用setOnKeyListener,每次都执行两次。最后上网看到别人的文章,解决了问题,解决方法是调用setOnEditorActionListener而不是用setOnKeyListener来监听点击搜索按钮。

代码如下(在fragment中写的,在activity中写的时候去掉context相关的东西就行了):

searchText.setOnEditorActionListener(new OnEditorActionListener() {

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId ==EditorInfo.IME_ACTION_SEARCH){

// 先隐藏键盘
((InputMethodManager) searchText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(
getActivity()
.getCurrentFocus()
.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);

//跳转activity
Intent intent = new Intent();
intent.setClass(getActivity(), SearchResultActivity.class);
startActivity(intent);

// 将查询的数据插入数据库

mDbHelper.insert_search_history(searchText.getText().toString(), getStringDate());

return true;
}
return false;
}
});

android设置软键盘搜索键以及监听搜索键点击时发生两次事件的问题解决

标签:

原文地址:http://www.cnblogs.com/xgjblog/p/5349839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!