如何使用最新的 WindowInset API 在出现软键盘时调整对话框布局 [英] How to adjust dialog layout when soft keyboard appears using the latest WindowInset API
问题描述
如何使用最新的WindowInset API来调整我的对话框和软键盘之间的空间?
How to use the latest WindowInset API to adjust space betweeen my dialog and softkeyboard?
我有一个带有一些 EditText 的 BottomSheetDialog.默认动画将在我的 EditText 正下方显示软键盘,它将覆盖我的保存按钮.经过一番研究,我将此行添加到我的 BottomSheetDialog
片段
I have a BottomSheetDialog with some EditText. The default animation will show the soft keyboard right below my EditText which will cover my save button. After doing some research, I added this line into my BottomSheetDialog
fragment
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
它奏效了(如下图所示)!
And it worked (as the picture is shown down below)!
但显然 SOFT_INPUT_ADJUST_RESIZE
已被弃用.
* @deprecated Call {@link Window#setDecorFitsSystemWindows(boolean)} with {@code false} and
* install an {@link OnApplyWindowInsetsListener} on your root content view that fits insets
* of type {@link Type#ime()}.
而且我不知道如何使用新的 OnApplyWindowInsetsListener
来达到同样的效果.这是我当前的 BottomSheetDialog
片段:
And I couldn't figure out how to use the new OnApplyWindowInsetsListener
to achieve the same effect.
Here is my current BottomSheetDialog
fragment:
public class BottomSheetDialog extends BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Adding this line works, but it's deprecated in API 30
// getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
getDialog().getWindow().setDecorFitsSystemWindows(false);
view = inflater.inflate(R.layout.fragment_bottom_dialog_cash, container, false);
view.setOnApplyWindowInsetsListener((v, insets) -> {
Log.d("dialog", "onCreateView: ");
Insets imeInsets = insets.getInsets(WindowInsets.Type.ime());
v.setPadding(0,0,0,imeInsets.bottom);
return insets;
});
return view;
}
我在另一个片段中使用 onclicklistener 来显示此对话框.另一个片段
I use an onclicklistener in another fragment to show this dialog. Code in Another fragment
@Override
public void onItemClick(int position) {
BottomSheetDialog dialog = new BottomSheetDialog();
dialog.show(getParentFragmentManager(), "BottomSheetDialog");
}
实际上,日志表明当软键盘弹出时从未触发侦听器.仅供参考,我正在关注此视频和这个博客.
In fact, the log indicates that the listener is never triggered when the soft keyboard pop up. FYI, I'm following this video and this blog.
推荐答案
经过更多研究,我发现如果我使用 viewBinding
并将 view
替换为 bind.getRoot()
,然后一切正常.我不知道为什么(也许我应该在 onViewCreated
中使用而不是 onCreateView
?)但代码应该对有同样问题的人有帮助.
After more research, I find out that if I use viewBinding
and replace view
with bind.getRoot()
, then everything works fine. I'm not sure why (maybe I should use in onViewCreated
instead of onCreateView
?) but the code should be helpful for people having the same issue.
// NOTE: you have to set this in the activity instead of fragment.
getWindow().setDecorFitsSystemWindows(false);
// Only work with API30 or above!
bind.getRoot().setOnApplyWindowInsetsListener((v, insets) -> {
imeHeight = insets.getInsets(WindowInsets.Type.ime()).bottom;
bind.getRoot().setPadding(0, 0, 0, imeHeight);
return insets;
});
需要注意的一点是 setDecorFitsSystemWindows(false) 意味着应用程序(您)负责所有系统窗口,包括状态栏和导航栏.
One thing to be noticed is that setDecorFitsSystemWindows(false) means the app (you) are responsible for all the system windows includes the status bar and navigation bar.
我也发现下面链接的教程非常有用,请检查您是否想了解更多关于 windowInsets 和新动画回调的信息.
I also find the tutorials linked down below are very useful, please check if you wanna know more about windowInsets and new animation callback.
用于检查键盘 (IME) 可见性和大小的新 WindowInsets API
这篇关于如何使用最新的 WindowInset API 在出现软键盘时调整对话框布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!