Android跟软键盘的故事
16lz
2022-01-09
1 软键盘弹出 EditText向上顶
方法一:只需要在 Androidmanifest中在对应的activity中添加
android:windowSoftInputMode="adjustResize|stateHidden"
方法二:在Activity中的oncreate中setContentView之前写上这个代码
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
2 监控软键盘确定 搜索 按钮并赋予点击事件
在android的实践开发中,为了界面的美观,往往那些搜索框并没有带搜索按钮,而是调用了软键盘的搜索按钮
首先在xml配置文件中添加属性属性: Android:imeOptions=”actionSearch”,这样我们调用软键盘时,回车键就会显示搜索二字。
EditText et_search= (EditText) findViewById(R.id.search_et);watchSearch() ;
//监控软件盘的搜索按钮public void watchSearch() {
et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {@Override public boolean onEditorAction (TextView v , int actionId , KeyEvent event) { if (actionId == EditorInfo. IME_ACTION_SEARCH ) { // 先隐藏键盘 ((InputMethodManager) et_search .getContext() .getSystemService(Context. INPUT_METHOD_SERVICE )) .hideSoftInputFromWindow(SearchActivity. this .getCurrentFocus().getWindowToken() , InputMethodManager. HIDE_NOT_ALWAYS ) ; /*
执行具体操作
*/ return true; } return false; } });}
3 输入之后点击按钮 软键盘消失
InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext(). getSystemService(Context.INPUT_METHOD_SERVICE);inputMethodManager.hideSoftInputFromWindow(et_address.getWindowToken(), 0);
//et_address为按钮上面的一个EditText
4点击EditText的时候弹出软键盘,点击EditText之外空白处软键盘消失
想要实现该功能只需要重写 public boolean onTouchEvent(MotionEvent event)方法
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
@Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (MakeActivity.this.getCurrentFocus() != null) { if (MakeActivity.this.getCurrentFocus().getWindowToken() != null) { imm.hideSoftInputFromWindow(MakeActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } } return super.onTouchEvent(event); }
更多相关文章
- 【android】点击事件穿透解决方案
- Android(安卓)UI编程基础3
- 系出名门Android(5) - 控件(View)之TextView, Button, ImageButt
- Github android客户端源代码分析之一:环境搭建
- 使用 Android快速开发框架 Afinal 0.3 快速开发网络应用相关APK
- Android动画学习笔记-Android(安卓)Animation
- Android之硬键盘字符映射------KeyCharacterMap
- android 不自动弹出软键盘
- 关于android软键盘弹出挤压VIEW和覆盖View的问题