如何判断一个Android设备是否具有硬键 [英] How to tell whether an android device has hard keys
本文介绍了如何判断一个Android设备是否具有硬键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何判断一个Android设备是否有物理按键或软件的导航栏?我需要改变布局依赖于导航软件是否绘制。
How can I tell whether an android device has physical keys or the software navigation bar? I need to change the layout dependant on whether the software navigation is drawn.
例如HTC Desire的C具有硬件按键:
For example the HTC Desire C has hardware keys:
我要澄清 - 林看着导航栏,而不是键盘。回到家,回等我试过:
I should clarify - Im looking at the navigation bar, not the keyboard. Home, back etc. I've tried:
getResources().getConfiguration().keyboard);
getResources().getConfiguration().navigation);
getResources().getConfiguration().navigationHidden);
返回两个设备上相同的值。
return the same values on both devices.
推荐答案
做这第一次应用程序启动并保存到preferences解决:
Solved by doing this the first time the app is launched and saving to preferences:
public static boolean hasSoftKeys(WindowManager windowManager){
boolean hasSoftwareKeys = true;
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设备是否具有硬键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文