[Android]搜索界面--自动弹出键盘,并点击键盘搜索进行搜索
16lz
2021-01-23
功能分析:
进入搜索页面,自动弹出键盘,并且可以点击键盘搜索按钮进行搜索,并自动隐藏键盘。
1.在XML在输入框中加入android:imeOptions="actionSearch"
2.自动弹出键盘 et_keyword = (EditText) findViewById(R.id.et_keyword);et_keyword.setSelected(true);et_keyword.requestFocus();//获得焦点
3.然后调用 OnEditorActionListener,不是OnKeyListener( import android.widget.TextView.OnEditorActionListener;)
et_keyword.setOnEditorActionListener(new OnEditorActionListener() {@Overridepublic boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {if (arg1 == EditorInfo.IME_ACTION_SEARCH) { //添加搜索事件 hideInput(mContext);//隐藏软键盘 }return false;}});
/** * 隐藏软键盘 */ private static InputMethodManager manager;// 输入法管理器 用户隐藏软键盘private void hideInput(Context context) {if(manager==null){manager = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE));}manager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);}
3.在androidMainfest.xml文件中在此Activity中写入 android:windowSoftInputMode="adjustPan"可以防止软键盘会把原来的界面挤上去的问题,stateVisible负责让键盘自动弹出
android:windowSoftInputMode="adjustPan|stateVisible"
更多相关文章
- Android 音频管理器AudioManager类介绍
- Android虚拟键盘挡住输入框的处理方法
- Android之EditView 阻止软键盘自动弹出
- Android O 物理键盘和虚拟键盘同时支持
- Android禁止软键盘弹出
- android 打开软键盘 关闭软键盘