简体   繁体   English

如何防止软键盘将我的视图向上推到片段中?

[英]How do I prevent the soft keyboard from pushing my view up in a fragment?

I have an edittext in my fragment.我的片段中有一个编辑文本。 To prevent soft keyboard from pushing my view up I tried为了防止软键盘将我的视图推高,我尝试了

android:windowSoftInputMode="adjustPan"
android:windowSoftInputMode="adjustNothing"

and programmatically i tried onResume and oncreateview methods of fragment并以编程方式我尝试了片段的 onResume 和 oncreateview 方法

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

Above techniques didn't worked.以上技术没有奏效。 But when I put scrollview as root view in fragment and put android:isScrollContainer="false" property then above methods are working... How can I make it work without scroll view但是当我将scrollview作为根视图放在片段中并放置android:isScrollContainer="false"属性时,上述方法正在工作......我如何在没有滚动视图的情况下使其工作

在片段getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN) onCreateView() 中使用它

Also, something to consider double-checking is if your fragment's are DialogFragments.此外,要考虑仔细检查您的片段是否为 DialogFragments。 Getting the window of my DialogFragment and then setting its soft input resolved my issue where the other solutions did not.获取我的 DialogFragment 的窗口,然后设置其软输入解决了我的问题,而其他解决方案则没有。

A dialog is a small window that prompts the user to make a decision or enter additional information.对话框是提示用户做出决定或输入附加信息的小窗口。 A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.对话框不会填满屏幕,通常用于需要用户在继续之前采取行动的模式事件。

Source: https://developer.android.com/guide/topics/ui/dialogs.html来源: https : //developer.android.com/guide/topics/ui/dialogs.html

That being said, DialogFragment's have their own windows that can be set by doing the following in your onCreateDialog method.话虽如此,DialogFragment 有自己的窗口,可以通过在 onCreateDialog 方法中执行以下操作来设置这些窗口。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    Dialog dialog = // instantiate your dialog

    //Get your dialog's window
    Window window = dialog.getWindow();

    if (window != null) {
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING); 
    }

    return dialog;
}

Setting android:isScrollContainer = "false" inside the ScrollView worked for me.在 ScrollView 中设置android:isScrollContainer = "false"对我有用。

According to the documentation, settings "isScrollContainer" to true means that the scroll view can be resized to shrink its overall window so that there is space for an input method.根据文档,将“isScrollContainer”设置为 true 意味着可以调整滚动视图的大小以缩小其整个窗口,以便为输入法留出空间。

I suspect what is happening without setting this attribute to false is that the ScrollView is being treated as a scroll container by default and is being resized (shrunk) to allow the input method to appear.我怀疑在没有将此属性设置为 false 的情况下发生的情况是,默认情况下 ScrollView 被视为滚动容器,并且正在调整大小(缩小)以允许输入法出现。 Everything outside of the ScrollView is shown because the ScrollView shrinks by as much as is necessary to show the input method and other views on the same hierarchy level.显示 ScrollView 之外的所有内容,因为 ScrollView 会根据需要缩小以在同一层次结构级别上显示输入法和其他视图。

If you only want to do this for specific fragments, do this:如果您只想对特定片段执行此操作,请执行以下操作:

  1. Save the current input mode保存当前输入模式
val inputMode = (activity as MainActivity).window.attributes.softInputMode
  1. Set the desired input mode in onCreateView()在 onCreateView() 中设置所需的输入模式
(activity as MainActivity).window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
  1. Set the input mode back to what it was before in onDestroy()在 onDestroy() 中将输入模式设置回之前的状态
override fun onDestroy() {
    super.onDestroy()
    inputMode?.let { (activity as MainActivity).window.setSoftInputMode(it)}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM