Android开发 关于软键盘的 打开与关闭
2015-10-23 17:39
405 查看
1、打开(显示软键盘)
//得到InputMethodManager的实例
InputMethodManager inputMethodManager=
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
打开软键盘(editText首先要获取焦点)
开始我也不会,于是就百度一下网上各种搜索,发现普遍都是如下所示:
//inputMethodManager.showSoftInputFromInputMethod(this.getCurrentFocus().getWindowToken(),
InputMethodManager.SHOW_IMPLICIT);
结果在模拟器和真机调试的时候,百般纳闷,因为根本就不起作用,键盘始终没有打开。至少Android4.2是如此,然后找到Android API,看看有没有别的方法可用,最终确定showSoftInput(View view,Int flags)方法是有效的,意思就是在某视图上打开软键盘输入区域。
inputMethodManager.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
2、关闭(隐藏软键盘)
和显示相同,我开始也是同网上大多数说法一样,用的是
//inputMethodManager.hideSoftInputFromInputMethod(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
同样没有效果,经查找API,方便的是,关闭软键盘有容易理解的方法可以使用:
//从当前窗口中隐藏软键盘
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
然而 显示软键盘 并没有 showSoftInputFromWindow方法。
3、切换
切换状态,顾名思义 也就是从显示到隐藏,从隐藏到显示软键盘。
if (inputMethodManager.isActive()) {
//切换
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
或
inputMethodManager.toggleSoftInputFromWindow(windowToken, showFlags, hideFlags);
都可以!
还是要多尝试,才有答案!
//得到InputMethodManager的实例
InputMethodManager inputMethodManager=
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
打开软键盘(editText首先要获取焦点)
开始我也不会,于是就百度一下网上各种搜索,发现普遍都是如下所示:
//inputMethodManager.showSoftInputFromInputMethod(this.getCurrentFocus().getWindowToken(),
InputMethodManager.SHOW_IMPLICIT);
结果在模拟器和真机调试的时候,百般纳闷,因为根本就不起作用,键盘始终没有打开。至少Android4.2是如此,然后找到Android API,看看有没有别的方法可用,最终确定showSoftInput(View view,Int flags)方法是有效的,意思就是在某视图上打开软键盘输入区域。
inputMethodManager.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
2、关闭(隐藏软键盘)
和显示相同,我开始也是同网上大多数说法一样,用的是
//inputMethodManager.hideSoftInputFromInputMethod(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
同样没有效果,经查找API,方便的是,关闭软键盘有容易理解的方法可以使用:
//从当前窗口中隐藏软键盘
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
然而 显示软键盘 并没有 showSoftInputFromWindow方法。
3、切换
切换状态,顾名思义 也就是从显示到隐藏,从隐藏到显示软键盘。
if (inputMethodManager.isActive()) {
//切换
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
或
inputMethodManager.toggleSoftInputFromWindow(windowToken, showFlags, hideFlags);
都可以!
还是要多尝试,才有答案!
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories