Android 设置软键盘搜索键以及监听搜索键点击事件

Android 设置软键盘搜索键以及监听搜索键点击事件

如图所示,有时候为了布局美观,在搜索时没有搜索按钮,而是调用软件盘上的按钮。调用的实现只需要在XML在输入框中加入android:imeOptions="actionSearch",另外,还要设置android:singleLine="true",保证点击不会换行,最后调用软键盘时,回车键就会显示搜索二字。

然后调用 OnEditorActionListener,不是OnKeyListener



[java] view plain copy
  1. et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
  2.     @Override  
  3.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  4.         if (actionId == EditorInfo.IME_ACTION_SEARCH){  
  5.             isSearch = true;  
  6.             page = 1;  
  7.             MyUtils.hideSoftKeyboard(EnterShopActivity.this,v);  
  8.             getData();  
  9.             return true;  
  10.         }  
  11.         return false;  
  12.     }  
  13. });  


在androidMainfest.xml文件中在此Activity中写入:

[java] view plain copy
  1. android:windowSoftInputMode="adjustPan"  

可以防止软键盘会把原来的界面挤上去的问题。


转自:http://blog.****.net/jyz_2015/article/details/51543318