gpt4 book ai didi

android - 如何判断安卓设备是否有硬键

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:34 26 4
gpt4 key购买 nike

如何判断 Android 设备是否有物理按键或软件导航栏?我需要根据是否绘制软件导航来更改布局。

例如 HTC Desire C 有硬件键: enter image description here

我应该澄清一下——我在看导航栏,而不是键盘。家,回来等。我试过:

        getResources().getConfiguration().keyboard);
getResources().getConfiguration().navigation);
getResources().getConfiguration().navigationHidden);

在两个设备上返回相同的值。

最佳答案

通过在第一次启动应用程序并保存到首选项时执行此操作来解决:

public static boolean hasSoftKeys(WindowManager windowManager){
boolean hasSoftwareKeys = true;
//c = context; use getContext(); in fragments, and in activities you can
//directly access the windowManager();

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN_MR1){
Display d = c.getWindowManager().getDefaultDisplay();

DisplayMetrics realDisplayMetrics = new DisplayMetrics();
d.getRealMetrics(realDisplayMetrics);

int realHeight = realDisplayMetrics.heightPixels;
int realWidth = realDisplayMetrics.widthPixels;

DisplayMetrics displayMetrics = new DisplayMetrics();
d.getMetrics(displayMetrics);

int displayHeight = displayMetrics.heightPixels;
int displayWidth = displayMetrics.widthPixels;

hasSoftwareKeys = (realWidth - displayWidth) > 0 ||
(realHeight - displayHeight) > 0;
} else {
boolean hasMenuKey = ViewConfiguration.get(c).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
hasSoftwareKeys = !hasMenuKey && !hasBackKey;
}
return hasSoftwareKeys;
}

关于android - 如何判断安卓设备是否有硬键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14853039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com