出现键盘时,Facebook 应用程序如何处理布局更改?
How does Facebook app handle layout changes when keyboard appears?
Facebook 应用程序在登录屏幕上处理 UI 更改的能力令人惊讶。看看吧。
无键盘
使用键盘(请注意需要帮助吗?和英语更改视图如何消失)
是的,我确实知道 adjustPan 和 adjustResize 设置,但是 Facebook 如何准确设置键盘输入时哪些视图可见在场吗?
edittext.post(new Runnable() {
public void run() {
edittext.requestFocusFromTouch();
InputMethodManager lManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
lManager.showSoftInput(edittext, 0);
}
});
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// view/hide whatever you want depending on the hasFocus value
// hasFocus == true -> keyboard shown
}
});
当软键盘可见时,它们似乎是 showing/hiding 特定文本(例如上面示例中的需要帮助或英语)。
Now, in Android, there is No direct way to detect if soft keyboard is visible or not.
为什么?
以下是Android框架工程师的回答。
The IME being shown has little meaning, since exactly how the IME
behaves is up to it -- it may be a transparent overlay and not impact
the application, a small strip, or all other kinds of things.
Due to this, the main way you interact with the IME is by setting your
softInputMode to be resizeable so when the IME says it wants to
occlude part of the screen your app's UI will get resized to take that
into account if needed.
但是,有不同的 ways/methods/workarounds 通过使用哪些应用程序(如 Facebook)可能会检测软键盘是否可见,并且根据结果,应用程序可以 show/hide texts/widgets里面是UI.
方法一:
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
//soft keyboard is shown, so hide "need help" text, for example
} else {
//Software Keyboard was not shown;
}
方法二:
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightView = activityRootView.getHeight();
int widthView = activityRootView.getWidth();
if (1.0 * widthView / heightView > 3) {
//Make changes for Keyboard not visible
} else {
//Make changes for keyboard visible
}
}
});
现在上面提到的两种方法不一定在所有情况下都能 100% 工作(正如人们可以从 SDK 中不受支持的操作方式所期望的那样)
Facebook 可能正在使用上述任何一种方法,或者谁知道呢,他们可能以不同的方式实现了它!
Facebook 应用程序在登录屏幕上处理 UI 更改的能力令人惊讶。看看吧。
无键盘
使用键盘(请注意需要帮助吗?和英语更改视图如何消失)
是的,我确实知道 adjustPan 和 adjustResize 设置,但是 Facebook 如何准确设置键盘输入时哪些视图可见在场吗?
edittext.post(new Runnable() {
public void run() {
edittext.requestFocusFromTouch();
InputMethodManager lManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
lManager.showSoftInput(edittext, 0);
}
});
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// view/hide whatever you want depending on the hasFocus value
// hasFocus == true -> keyboard shown
}
});
当软键盘可见时,它们似乎是 showing/hiding 特定文本(例如上面示例中的需要帮助或英语)。
Now, in Android, there is No direct way to detect if soft keyboard is visible or not.
为什么?
以下是Android框架工程师的回答。
The IME being shown has little meaning, since exactly how the IME behaves is up to it -- it may be a transparent overlay and not impact the application, a small strip, or all other kinds of things.
Due to this, the main way you interact with the IME is by setting your softInputMode to be resizeable so when the IME says it wants to occlude part of the screen your app's UI will get resized to take that into account if needed.
但是,有不同的 ways/methods/workarounds 通过使用哪些应用程序(如 Facebook)可能会检测软键盘是否可见,并且根据结果,应用程序可以 show/hide texts/widgets里面是UI.
方法一:
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
//soft keyboard is shown, so hide "need help" text, for example
} else {
//Software Keyboard was not shown;
}
方法二:
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightView = activityRootView.getHeight();
int widthView = activityRootView.getWidth();
if (1.0 * widthView / heightView > 3) {
//Make changes for Keyboard not visible
} else {
//Make changes for keyboard visible
}
}
});
现在上面提到的两种方法不一定在所有情况下都能 100% 工作(正如人们可以从 SDK 中不受支持的操作方式所期望的那样)
Facebook 可能正在使用上述任何一种方法,或者谁知道呢,他们可能以不同的方式实现了它!