Android开发 - 获取系统输入法高度的正确姿势
问题与解决
在Android应用的开发中,有一些需求需要我们获取到输入法的高度,但是官方的API并没有提供类似的方法,所以我们需要自己来实现。
查阅了网上很多资料,试过以后都不理想。
比如有的方法通过监听布局的变化来计算输入法的高度,这种方式在Activity的配置中配置为"android:windowSoftInputMode="adjustResize""时没有问题,可以正确获取输入法的高度,因为布局此时确实会动态的调整。
但是当Activity配置为"android:windowSoftInputMode="adjustNothing""时,布局不会在输入法弹出时进行调整,上面的方式就会扑街。
不过经过一番探索和测试,终于发现了一种方式可以在即使设置为adjustNothing时也可以正确计算高度放方法。
同时也感谢这位外国朋友:
GitHub地址
其实也就两个类,我也做了一些修改,解决了一些问题,这里也贴出来:
- KeyboardHeightObserver.java
/**
* The observer that will be notified when the height of
* the keyboard has changed
*/
public interface KeyboardHeightObserver {
/**
* Called when the keyboard height has changed, 0 means keyboard is closed,
* >= 1 means keyboard is opened.
*
* @param height The height of the keyboard in pixels
* @param orientation The orientation either: Configuration.ORIENTATION_PORTRAIT or
* Configuration.ORIENTATION_LANDSCAPE
*/
void onKeyboardHeightChanged(int height, int orientation);
}
- KeyboardHeightProvider.java
/**
* The keyboard height provider, this class uses a PopupWindow
* to calculate the window height when the floating keyboard is opened and closed.
*/
public class KeyboardHeightProvider extends PopupWindow {
/** The tag for logging purposes */
private final static String TAG = "sample_KeyboardHeightProvider";
/** The keyboard height observer */
private KeyboardHeightObserver observer;
/** The cached landscape height of the keyboard */
private int keyboardLandscapeHeight;
/** The cached portrait height of the keyboard */
private int keyboardPortraitHeight;
/** The view that is used to calculate the keyboard height */
private View popupView;
/** The parent view */
private View parentView;
/** The root activity that uses this KeyboardHeightProvider */
private Activity activity;
/**
* Construct a new KeyboardHeightProvider
*
* @param activity The parent activity
*/
public KeyboardHeightProvider(Activity activity) {
super(activity);
this.activity = activity;
LayoutInflater inflator = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
this.popupView = inflator.inflate(R.layout.keyboard_popup_window, null, false);
setContentView(popupView);
setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_RESIZE | LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
parentView = activity.findViewById(android.R.id.content);
setWidth(0);
setHeight(LayoutParams.MATCH_PARENT);
popupView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (popupView != null) {
handleOnGlobalLayout();
}
}
});
}
/**
* Start the KeyboardHeightProvider, this must be called after the onResume of the Activity.
* PopupWindows are not allowed to be registered before the onResume has finished
* of the Activity.
*/
public void start() {
if (!isShowing() && parentView.getWindowToken() != null) {
setBackgroundDrawable(new ColorDrawable(0));
showAtLocation(parentView, Gravity.NO_GRAVITY, 0, 0);
}
}
/**
* Close the keyboard height provider,
* this provider will not be used anymore.
*/
public void close() {
this.observer = null;
dismiss();
}
/**
* Set the keyboard height observer to this provider. The
* observer will be notified when the keyboard height has changed.
* For example when the keyboard is opened or closed.
*
* @param observer The observer to be added to this provider.
*/
public void setKeyboardHeightObserver(KeyboardHeightObserver observer) {
this.observer = observer;
}
/**
* Get the screen orientation
*
* @return the screen orientation
*/
private int getScreenOrientation() {
return activity.getResources().getConfiguration().orientation;
}
/**
* Popup window itself is as big as the window of the Activity.
* The keyboard can then be calculated by extracting the popup view bottom
* from the activity window height.
*/
private void handleOnGlobalLayout() {
Point screenSize = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(screenSize);
Rect rect = new Rect();
popupView.getWindowVisibleDisplayFrame(rect);
// REMIND, you may like to change this using the fullscreen size of the phone
// and also using the status bar and navigation bar heights of the phone to calculate
// the keyboard height. But this worked fine on a Nexus.
int orientation = getScreenOrientation();
int keyboardHeight = screenSize.y - rect.bottom;
if (keyboardHeight == 0) {
notifyKeyboardHeightChanged(0, orientation);
}
else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
this.keyboardPortraitHeight = keyboardHeight;
notifyKeyboardHeightChanged(keyboardPortraitHeight, orientation);
}
else {
this.keyboardLandscapeHeight = keyboardHeight;
notifyKeyboardHeightChanged(keyboardLandscapeHeight, orientation);
}
}
private void notifyKeyboardHeightChanged(int height, int orientation) {
if (observer != null) {
observer.onKeyboardHeightChanged(height, orientation);
}
}
}
使用方法
此处以在Activity中的使用进行举例。
实现接口
引入这两个类后,在当前Activity中实现接口KeyboardHeightObserver:
@Override
public void onKeyboardHeightChanged(int height, int orientation) {
String or = orientation == Configuration.ORIENTATION_PORTRAIT ? "portrait" : "landscape";
Logger.d(TAG, "onKeyboardHeightChanged in pixels: " + height + " " + or);
}
定义并初始化
在当前Activity定义成员变量,并在onCreate()中进行初始化
private KeyboardHeightProvider mKeyboardHeightProvider;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
...
mKeyboardHeightProvider = new KeyboardHeightProvider(this);
new Handler().post(() -> mKeyboardHeightProvider.start());
}
生命周期处理
初始化完成后,我们要在Activity中的生命周期中也要进行处理,以免内存泄露。
@Override
protected void onResume() {
super.onResume();
mKeyboardHeightProvider.setKeyboardHeightObserver(this);
}
@Override
protected void onPause() {
super.onPause();
mKeyboardHeightProvider.setKeyboardHeightObserver(null);
}
@Override
protected void onDestroy() {
super.onDestroy();
mKeyboardHeightProvider.close();
}
总结
此时我们就可以正确获取的当前输入法的高度了,即使android:windowSoftInputMode="adjustNothing"时也可以正确获取到,这正是这个方法的强大之处,利用这个方法可以实现比如类似微信聊天的界面,流畅切换输入框,表情框等。
如有更多疑问,请参考我的其它Android相关博客:我的博客地址
Android开发 - 获取系统输入法高度的正确姿势的更多相关文章
- Android开发 - 获取Android设备的唯一标识码(Android 6.0或更高)
在我们的APP开发中,通常需要获取到设备的唯一标识.在Android6.0之前,有很多方法我们可以方便获取到硬件的唯一标识,但是在Android6.0之后,Android系统大幅限制了我们获取设备的硬 ...
- android开发中系统自带语音模块的使用
android开发中系统自带语音模块的使用需求:项目中需要添加语音搜索模块,增加用户体验解决过程:在网上搜到语音搜索例子,参考网上代码,加入到了自己的项目,完成产品要求.这个问题很好解决,网上能找到很 ...
- android开发获取网络状态,wifi,wap,2g,3g.工具类(一)
android开发获取网络状态整理: package com.gzcivil.utils; import android.content.Context; import android.net.Con ...
- Android中获取系统上安装的APP信息
Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:00000099 EndFragment:00003259 Android中获取系统上安装的APP信 ...
- 基于jeesite+android开发 电子商务系统免费教程
下载地址: jeesite免费教程 基于jeesite+android开发 电子商务系统免费教程 基于jeesite+android开发 电子商务系统免费教程 这个教程已经录制完很久了,一直没有公开, ...
- Android 开发 获取Android设备的屏幕高宽
获得屏幕的宽度和高度有很多种方法: //1.通过WindowManager获取 DisplayMetrics dm = new DisplayMetrics(); heigth = dm.height ...
- android:sharedUserId 获取系统权限
最近在做的项目,有好大一部分都用到这个权限,修改系统时间啊,调用隐藏方法啊,系统关机重启啊,静默安装升级卸载应用等等,刚开始的时候,直接添加权限,运行就报错,无论模拟器还是真机,在logcat中总会得 ...
- Android apk获取系统权限
Android在apk内部,即通过java代码来进行修改系统文件或者修改系统设置等等,这样需要获取系统权限. 通过直接配置apk运行在System进程内 1. 在应用程序的AndroidManifes ...
- Android项目--获取系统通讯录列表
----------------- 通讯录列表 ----------------- 按常理来说,获取系统通讯录列表,无非就是将通讯录的数据库打开获取数据,适配,添加即可. Cursor cursor; ...
随机推荐
- g2o相关问题cs.h,以及no matching function for call to ‘g2o::OptimizationAlgorithmLevenberg::OptimizationAlgorithmLevenberg(Block*&)
1.对于cs.h找不到的情况 1)编译的时候一定要把csparse在EXTERNAL文件中,编译进去. 2)修改CMakeLists.txt文件中的include_directories中的${CPA ...
- SparkStreaming 监控文件目录
SparkStream 监控文件目录时,只能监控文件内是否添加新的文件,如果文件名没有改变只是文件内容改变,那么不会检测出有文件进行了添加. )) )).reduceByKey(_ + _) word ...
- 2017/2/13:springMVC拦截器的使用
一.定义Interceptor实现类(主要是实现Handlerceptor接口) SpringMVC 中的Interceptor 拦截请求是通过HandlerInterceptor 来实现的.在Spr ...
- H.264, MPEG4之间的关系
百度百科搜索 H.264 H.264是国际标准化组织(ISO)和国际电信联盟(ITU)共同提出的继MPEG4之后的新一代数字视频压缩格式.H.264是ITU-T以H.26x系列为名称命名的视频编解码技 ...
- Sharing Code Between Silverlight and WPF
一个很好的列子: http://www.codeproject.com/Articles/254506/XAMLFinance-A-Cross-platform-WPF-Silverlight-WP7 ...
- [C#]“正在终止线程”的问题
在C#中启用线程后,如果试图使用Abort方法来终止线程,那么必定会抛出“正在终止线程”的异常,一开始我也想过如何来避免这种异常出现,花了不少气力,但最后发现全是徒劳. 原因是一个正在运行的线程被终止 ...
- [ASP.NET]使用Layer简介
layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会轻松地拥有丰富友好的操作体验. 在与同类组件的比较中,layer总是能轻易获胜.她尽可能 ...
- pat1079+1086+1090+1094(树的遍历)感想
今天做了这4道题,虽然大部分以前做过,但还是有些知识掌握不全. 总结一下所用的树的知识及解决方法 (1)非二叉树的遍历: 非二叉树就是图,所以它的存储结构类似邻接表,c++提供了vector数组可以很 ...
- pytorch总结
PyTorch 深度学习:60分钟快速入门 用例子学习 PyTorch 手把手教你用PyTorch从零搭建图像分类模型
- UVaLive 3641 Leonardo's Notebook (置换)
题意:给定一个置换 B 问是否则存在一个置换 A ,使用 A^2 = B. 析:可以自己画一画,假设 A = (a1, a2, a3)(b1, b2, b3, b4),那么 A^2 = (a1, a2 ...