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

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

时间:2014-08-22 23:51:29      阅读:406      评论:0      收藏:0      [点我收藏+]

标签:textview   xml   search   

bubuko.com,布布扣

如图所示,有时候为了布局美观,在搜索时没有搜索按钮,而是调用软件盘上的按钮。调用的实现只需要在XML在输入框中加入android:imeOptions="actionSearch",调用软键盘时,回车键就会显示搜索二字。

然后调用 OnEditorActionListener,不是OnKeyListener

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);

                     

                   return true;
                   }
               return false;
           }
});

在androidMainfest.xml文件中在此Activity中写入 android:windowSoftInputMode="adjustPan"可以防止软键盘会把原来的界面挤上去的问题


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

标签:textview   xml   search   

原文地址:http://blog.csdn.net/jia635/article/details/38766255

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