/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
* <p>
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package xiaxl.custom.keyboard.listener;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Holds an instance of the current DisplayMetrics so we don't have to thread it through all the
* classes that need it.
* Note: windowDisplayMetrics are deprecated in favor of ScreenDisplayMetrics: window metrics
* are supposed to return the drawable area but there's no guarantee that they correspond to the
* actual size of the {@link android.app.Activity}'s RootView. Moreover, they are not consistent with what iOS
* returns. Screen metrics returns the metrics of the entire screen, is consistent with iOS and
* should be used instead.
*/
public class CustomDisplayMetricsHolder {
private static @Nullable
DisplayMetrics sWindowDisplayMetrics;
private static @Nullable DisplayMetrics sScreenDisplayMetrics;
/**
* @deprecated Use {@link #setScreenDisplayMetrics(DisplayMetrics)} instead. See comment above as
* to why this is not correct to use.
*/
public static void setWindowDisplayMetrics(DisplayMetrics displayMetrics) {
sWindowDisplayMetrics = displayMetrics;
}
public static void initDisplayMetricsIfNotInitialized(Context context) {
if (CustomDisplayMetricsHolder.getScreenDisplayMetrics() != null) {
return;
}
initDisplayMetrics(context);
}
public static void initDisplayMetrics(Context context) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
CustomDisplayMetricsHolder.setWindowDisplayMetrics(displayMetrics);
DisplayMetrics screenDisplayMetrics = new DisplayMetrics();
screenDisplayMetrics.setTo(displayMetrics);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (wm == null) throw new AssertionError();
Display display = wm.getDefaultDisplay();
// Get the real display metrics if we are using API level 17 or higher.
// The real metrics include system decor elements (e.g. soft menu bar).
//
// See: http://developer.android.com/reference/android/view/Display.html#getRealMetrics(android.util.DisplayMetrics)
if (Build.VERSION.SDK_INT >= 17) {
display.getRealMetrics(screenDisplayMetrics);
} else {
// For 14 <= API level <= 16, we need to invoke getRawHeight and getRawWidth to get the real dimensions.
// Since react-native only supports API level 16+ we don't have to worry about other cases.
//
// Reflection exceptions are rethrown at runtime.
//
// See: http://stackoverflow.com/questions/14341041/how-to-get-real-screen-height-and-width/23861333#23861333
try {
Method mGetRawH = Display.class.getMethod("getRawHeight");
Method mGetRawW = Display.class.getMethod("getRawWidth");
screenDisplayMetrics.widthPixels = (Integer) mGetRawW.invoke(display);
screenDisplayMetrics.heightPixels = (Integer) mGetRawH.invoke(display);
} catch (InvocationTargetException e) {
throw new RuntimeException("Error getting real dimensions for API level < 17", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Error getting real dimensions for API level < 17", e);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Error getting real dimensions for API level < 17", e);
}
}
CustomDisplayMetricsHolder.setScreenDisplayMetrics(screenDisplayMetrics);
}
/**
* @deprecated Use {@link #getScreenDisplayMetrics()} instead. See comment above as to why this
* is not correct to use.
*/
@Deprecated
public static DisplayMetrics getWindowDisplayMetrics() {
return sWindowDisplayMetrics;
}
public static void setScreenDisplayMetrics(DisplayMetrics screenDisplayMetrics) {
sScreenDisplayMetrics = screenDisplayMetrics;
}
public static DisplayMetrics getScreenDisplayMetrics() {
return sScreenDisplayMetrics;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
监听Android系统软键盘的弹出与关闭(案例代码)
共67个文件
xml:16个
webp:10个
ds_store:8个
需积分: 5 1 下载量 131 浏览量
2023-06-20
14:13:31
上传
评论
收藏 309KB ZIP 举报
温馨提示
代码来源: 从facebook React源码ReactRootView中抽离CustomGlobalLayoutListener,用于监听Android系统软键盘的弹出与关闭。 实现原理: 监听Android DecorView对应的GlobalLayout布局变化,从而判断是否弹出了软键盘。 其他: 案例demo名称为GlobalKeyboardChange,可直接运行; 软键盘弹出和关闭时,可同时回调 keyboardHeight 软键盘的高度值。
资源推荐
资源详情
资源评论
收起资源包目录
GlobalKeyboardChange.zip (67个子文件)
GlobalKeyboardChange
.gradle
buildOutputCleanup
cache.properties 52B
buildOutputCleanup.lock 17B
outputFiles.bin 21KB
4.10.1
javaCompile
taskHistory.bin 23KB
classAnalysis.bin 61KB
javaCompile.lock 17B
fileChanges
last-build.bin 1B
taskHistory
taskHistory.bin 1MB
taskHistory.lock 17B
vcsMetadata-1
fileContent
fileContent.lock 17B
fileHashes
fileHashes.lock 17B
resourceHashesCache.bin 18KB
fileHashes.bin 128KB
gc.properties 0B
vcs-1
gc.properties 0B
gradle.properties 728B
.DS_Store 6KB
gradle
wrapper
gradle-wrapper.jar 53KB
gradle-wrapper.properties 233B
app
.DS_Store 6KB
src
.DS_Store 6KB
main
.DS_Store 6KB
java
.DS_Store 6KB
xiaxl
.DS_Store 6KB
custom
.DS_Store 6KB
keyboard
.DS_Store 6KB
MainActivity.java 1KB
listener
CustomDisplayMetricsHolder.java 5KB
CustomGlobalKeyboardLayoutListener.java 3KB
OnCustomGlobalKeyboardChangedListener.java 564B
res
mipmap-xxhdpi
ic_launcher_round.webp 6KB
ic_launcher.webp 3KB
mipmap-hdpi
ic_launcher_round.webp 3KB
ic_launcher.webp 1KB
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-anydpi-v26
ic_launcher.xml 272B
ic_launcher_round.xml 272B
mipmap-mdpi
ic_launcher_round.webp 2KB
ic_launcher.webp 982B
mipmap-xxxhdpi
ic_launcher_round.webp 8KB
ic_launcher.webp 4KB
mipmap-xhdpi
ic_launcher_round.webp 4KB
ic_launcher.webp 2KB
mipmap-anydpi-v33
ic_launcher.xml 343B
values
ic_launcher_background.xml 120B
colors.xml 208B
strings.xml 83B
styles.xml 383B
layout
activity_main.xml 521B
drawable
ic_launcher_background.xml 5KB
AndroidManifest.xml 715B
proguard-rules.pro 751B
libs
build.gradle 713B
.gitignore 7B
GlobalKeyboardChanged 700B
gradlew.bat 2KB
build.gradle 558B
.idea
jarRepositories.xml 2KB
workspace.xml 5KB
misc.xml 468B
compiler.xml 169B
gradle.xml 680B
.gitignore 47B
settings.gradle 15B
local.properties 404B
gradlew 5KB
.gitignore 87B
共 67 条
- 1
资源评论
bjxiaxueliang
- 粉丝: 1w+
- 资源: 47
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功