Android:如何防止软键盘将我的视图向上推?

发布于 2024-10-03 08:34:10 字数 108 浏览 5 评论 0原文

我的应用程序底部有一个垂直滑动抽屉。当软键盘打开时,它会将抽屉的标签向上推,使其位于键盘顶部。我实际上希望它保留在屏幕底部,在显示键盘时隐藏。

还有其他人遇到这个问题吗?知道如何解决吗?

I have a vertical sliding drawer at the bottom of my app. When the soft keyboard opens, it pushes the tab for the drawer up, so it sits atop the keyboard. I actually want it to remain at the bottom of the screen, becoming hidden when the keyboard is shown.

Anyone else run into this issue? Know how to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

皇甫轩 2024-10-10 08:34:10

您只需在 Activity 标记内的 AndroidMainfest.xml 文件中将 Activity 的 windowSoftInputMode 标志切换为 adjustPan 即可。

有关详细信息,请查看官方文档

<activity
   ...
   android:windowSoftInputMode="adjustPan"> 
</activity>

如果您的容器没有改变大小,那么您可能将高度设置为“匹配父级”。如果可能,将父级设置为“Wrap Content”,或将约束布局设置为父级的顶部和底部。

父容器将缩小以适应可用空间,因此您的内容可能应该位于滚动视图内,以防止(取决于手机制造商和选择的布局...)

  1. 内容被粉碎在一起
  2. 内容挂在屏幕上
  3. 由于内容位于键盘下方而无法访问,

即使其所在的布局是相对或约束布局,该内容也可能会出现问题 1-3。

You can simply switch your Activity's windowSoftInputModeflag to adjustPan in your AndroidMainfest.xml file inside your activity tag.

Check the official documentation for more info.

<activity
   ...
   android:windowSoftInputMode="adjustPan"> 
</activity>

If your container is not changing size, then you likely have the height set to "match parent". If possible, set the parent to "Wrap Content", or a constraint layout with constraingts to top and bottom of parent.

The parent container will shrink to fit the available space, so it is likely that your content should be inside of a scolling view to prevent (depending on the phone manufacturer and the layout choosen...)

  1. Content being smashed together
  2. Content hanging off the screen
  3. Content being inacccessable due to it being underneath the keyboard

even if the layout it is in is a relative or constraint layout, the content could exhibit problems 1-3.

当梦初醒 2024-10-10 08:34:10

所有答案都不适合我,但这确实有效,将此属性添加到 AndroidManifest.xml 中的 activity 标记中:

<activity
     ...
   android:windowSoftInputMode="adjustNothing"> 
</activity>

None of the answers worked for me, but this did the trick, add this attribute to the activity tag in your AndroidManifest.xml:

<activity
     ...
   android:windowSoftInputMode="adjustNothing"> 
</activity>
抹茶夏天i‖ 2024-10-10 08:34:10

就我而言,按钮被推起的原因是因为它们上方的视图是一个 ScrollView,并且无论 android 的值是什么,它都会随着键盘上方推起的按钮而折叠: windowSoftInputMode 我正在设置。

通过在按钮上方的 ScrollView 上设置 android:isScrollContainer="false" ,我能够避免底行按钮被软键盘推起。

In my case, the reason the buttons got pushed up was because the view above them was a ScrollView, and it got collapsed with the buttons pushed up above the keyboard no matter what value of android:windowSoftInputMode I was setting.

I was able to avoid my bottom row of buttons getting pushed up by the soft keyboard by setting android:isScrollContainer="false" on the ScrollView that sits above the buttons.

日久见人心 2024-10-10 08:34:10

您可以尝试动态添加此属性,方法是将以下代码放入活动的 onCreate 方法中:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

这对我有用,但那:

android:windowSoftInputMode="adjustPan"

没有。

You can try to add this attribute dynamically, by putting the following code in the onCreate method of your activity:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

This worked for me, but that:

android:windowSoftInputMode="adjustPan"

didnt.

陪我终i 2024-10-10 08:34:10

这里的这些答案对我没有帮助。所以我尝试了这个:

android:windowSoftInputMode="adjustResize"

这就像一个魅力,现在我的应用程序的标题不会消失。其更平滑。

These answers here didn't help me. So I tried this:

android:windowSoftInputMode="adjustResize"

This worked like a charm, Now the header of my app doesn't disappear. Its smoother.

做个ˇ局外人 2024-10-10 08:34:10

要在片段中以编程方式执行此操作,您可以使用以下代码

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

将其放在 onResume() 中

To do this programatically in a fragment you can use following code

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

Place this in onResume()

宁愿没拥抱 2024-10-10 08:34:10

这对我有用

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

This one worked for me

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
反话 2024-10-10 08:34:10

只需添加一行...

manifest 文件 的所需活动中添加 android:windowSoftInputMode="stateHidden|adjustPan"

我刚刚解决了:):)

Just a single line to be added...

Add android:windowSoftInputMode="stateHidden|adjustPan" in required activity of your manifest file.

I just got solved :) :)

卖梦商人 2024-10-10 08:34:10

对于未来的读者。

我想要对此问题进行具体控制,所以这就是我所做的:

从片段或活动中,隐藏您的其他视图(键盘打开时不需要这些视图) ),然后恢复它们来解决这个问题:

            rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    rootView.getWindowVisibleDisplayFrame(r);
                    int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top);

                    if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                    //ok now we know the keyboard is up...
                        view_one.setVisibility(View.GONE);
                        view_two.setVisibility(View.GONE);

                    }else{
                    //ok now we know the keyboard is down...
                        view_one.setVisibility(View.VISIBLE);
                        view_two.setVisibility(View.VISIBLE);

                    }
                }
            });

For future readers.

I wanted specific control over this issue, so this is what I did:

From a fragment or activity, hide your other views (that aren't needed while the keyboard is up), then restore them to solve this problem:

            rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    rootView.getWindowVisibleDisplayFrame(r);
                    int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top);

                    if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                    //ok now we know the keyboard is up...
                        view_one.setVisibility(View.GONE);
                        view_two.setVisibility(View.GONE);

                    }else{
                    //ok now we know the keyboard is down...
                        view_one.setVisibility(View.VISIBLE);
                        view_two.setVisibility(View.VISIBLE);

                    }
                }
            });
○愚か者の日 2024-10-10 08:34:10

到目前为止,答案对我没有帮助,因为我在 textView 下面有一个按钮和一个 textInput 字段(并排),它一直被键盘隐藏,但这已经解决了我的问题:

android:windowSoftInputMode="adjustResize"

So far the answers didn't help me as I have a button and a textInput field (side by side) below the textView which kept getting hidden by the keyboard, but this has solved my issue:

android:windowSoftInputMode="adjustResize"
愛放△進行李 2024-10-10 08:34:10

对于xamarin用户,将此代码添加到MainActivity类的Activity属性中,

WindowSoftInputMode =Android.Views.SoftInput.AdjustNothing

或者您可以将此代码Window.SetSoftInputMode(Android.Views.SoftInput.AdjustNothing)添加到MainActivity类的OnCreate方法中。

For xamarin users add this code to Activity attribute of the MainActivity class,

WindowSoftInputMode =Android.Views.SoftInput.AdjustNothing

or you can add this code Window.SetSoftInputMode(Android.Views.SoftInput.AdjustNothing) to the OnCreate method of MainActivity class.

忆离笙 2024-10-10 08:34:10

这是最适合我的,

android:windowSoftInputMode="adjustNothing"

试试吧!

This was the best which worked for me

android:windowSoftInputMode="adjustNothing"

Try it!

兮子 2024-10-10 08:34:10

将以下代码添加到清单文件的“活动”中。

android:windowSoftInputMode="adjustResize"

Add following code to the 'activity' of Manifest file.

android:windowSoftInputMode="adjustResize"
云淡月浅 2024-10-10 08:34:10
android:windowSoftInputMode="stateHidden|adjustNothing"

这段代码有效。

android:windowSoftInputMode="stateHidden|adjustNothing"

This code works.

萌逼全场 2024-10-10 08:34:10

好吧,我已经看过这些答案,但就我而言,我遇到了同样的问题,并通过一个非常方便且最简单的解决方案获得了庇护,该解决方案涉及在 xml 文件中的 Scrollview 标记中放置一个非常小的无辜属性。那就是

android:isScrollContainer="false"

祝你好运!

Well i have watched these answers but in my case i fell into the same issue and got refuge through a very handy and easiest solution that involves putting a very small innocent attribute in your Scrollview tag residing in your xml file. That is

android:isScrollContainer="false"

Good luck!

左秋 2024-10-10 08:34:10
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

这个对我有用。

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

This one is working for me.

情感失落者 2024-10-10 08:34:10

我通过在清单文件中添加添加解决了我的问题

 android:windowSoftInputMode="adjustNothing" 

并将 Recyclerviews 约束 isScrollContainer 设置为 false 。

android:isScrollContainer="false"

I have solved my issue by adding

 android:windowSoftInputMode="adjustNothing" 

In manifest file add.

and making the Recyclerviews constraint isScrollContainer to false .

android:isScrollContainer="false"
来日方长 2024-10-10 08:34:10

有两种方法可以解决这个问题。
转到 AndroidManifist.xml,并在您的活动名称中添加此行

   android:windowSoftInputMode="adjustPan"

如下面的代码所示,我已添加到注册活动。

 <activity android:name=".Register"
            android:windowSoftInputMode="adjustPan">

在第二种方式中,转到您的活动,然后在 onCreate 方法中添加此代码。

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

There are two ways of solving this problem.
Go to the AndroidManifist.xml, and in the name of your activity, add this line

   android:windowSoftInputMode="adjustPan"

As in the below code, I have added to the Register Activity.

 <activity android:name=".Register"
            android:windowSoftInputMode="adjustPan">

In the second way, go to your activity, and in your onCreate method, add this code.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
醉生梦死 2024-10-10 08:34:10

尝试使用这个:

android:windowSoftInputMode="stateHidden|adjustPan"

Try to use this:

android:windowSoftInputMode="stateHidden|adjustPan"
水染的天色ゝ 2024-10-10 08:34:10

对于滚动视图:

如果在 Android Manifest 中添加 android:windowSoftInputMode="stateHidden|adjustPan" 后仍然不起作用。

它可能会受到影响,因为当键盘出现时,它将进入滚动视图,如果您的按钮/任何对象不在滚动视图中,那么这些对象将跟随键盘并移动其位置。

检查按钮所在的 xml,并确保它位于滚动视图括号下方而不是超出滚动视图括号之外。

希望这会有所帮助。 :D

For Scroll View:

if after adding android:windowSoftInputMode="stateHidden|adjustPan" in your Android Manifest and still does not work.

It may be affected because when the keyboard appears, it will be into a scroll view and if your button/any objects is not in your scroll view then the objects will follow the keyboard and move its position.

Check out your xml where your button is and make sure it is under your scroll View bracket and not out of it.

Hope this helps out. :D

雪化雨蝶 2024-10-10 08:34:10

就我而言,我需要键盘保持隐藏状态,并且在单击按钮后我的布局需要调整,所以我只是在清单中添加了这个命令,它变得非常正确。

android:windowSoftInputMode="stateHidden|adjustResize"

In my case I needed the keyboard to stay hidden and just after the click of the button my layout needs to be adjusted, so I just added this command in the manifest and it got super right.

android:windowSoftInputMode="stateHidden|adjustResize"
望喜 2024-10-10 08:34:10

当您想在打开键盘时隐藏视图时。

将其添加到清单文件中的活动中

android:windowSoftInputMode="stateHidden|adjustPan"

When you want to hide view when open keyboard.

Add this into your Activity in manifest file

android:windowSoftInputMode="stateHidden|adjustPan"
魂牵梦绕锁你心扉 2024-10-10 08:34:10

他们都不适合我,试试这个

 private void scrollingWhileKeyboard() {
    drawerlayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            Rect r = new Rect();
            try {
                drawerlayout.getWindowVisibleDisplayFrame(r);
                int screenHeight = drawerlayout.getRootView().getHeight();
                int keypadHeight = screenHeight - r.bottom;
                if (keypadHeight > screenHeight * 0.15) {
                    tabLayout.setVisibility(View.GONE);
                } else {
                    tabLayout.setVisibility(View.VISIBLE);
                }
            } catch (NullPointerException e) {

            }
        }
    });

}

None of them worked for me, try this one

 private void scrollingWhileKeyboard() {
    drawerlayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            Rect r = new Rect();
            try {
                drawerlayout.getWindowVisibleDisplayFrame(r);
                int screenHeight = drawerlayout.getRootView().getHeight();
                int keypadHeight = screenHeight - r.bottom;
                if (keypadHeight > screenHeight * 0.15) {
                    tabLayout.setVisibility(View.GONE);
                } else {
                    tabLayout.setVisibility(View.VISIBLE);
                }
            } catch (NullPointerException e) {

            }
        }
    });

}
烟酉 2024-10-10 08:34:10

我为这个问题苦苦挣扎了一段时间。一些解决方案有效,但是我的一些观点仍然被推崇,而另一些则没有......所以它并没有完全解决我的问题。最后,这项工作的作用是将以下代码行添加到我的活动标记中的清单中......

android:windowSoftInputMode="stateHidden|adjustPan|adjustResize"

祝你好运

I was struggling for a while with this problem. Some of the solutions worked however some of my views where still being pushed up while others weren't... So it didn't completely solve my problem. In the end, what did the job was adding the following line of code to my manifest in the activity tag...

android:windowSoftInputMode="stateHidden|adjustPan|adjustResize"

Good luck

往昔成烟 2024-10-10 08:34:10

该活动的主窗口不会调整大小以为软键盘腾出空间。相反,窗口的内容将自动平移,以便当前焦点永远不会被键盘遮挡,并且用户始终可以看到他们正在键入的内容。

android:windowSoftInputMode="adjustPan"

这可能是您想要的更好的解决方案。

The activity's main window will not resize to make room for the soft keyboard. Rather, the contents of the window will be automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing.

android:windowSoftInputMode="adjustPan"

This might be a better solution for what you desired.

全部不再 2024-10-10 08:34:10

这段代码可能对你有帮助。在您的 oncreate 方法中使用它。

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

This code may help you. Use it in your oncreate method.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
昵称有卵用 2024-10-10 08:34:10

包含在您要显示的活动下的清单文件中。但请确保不使用全屏活动

android:windowSoftInputMode="adjustPan"

Include in your manifest file under activity which you want to display .But make sure not using Full screen Activity

android:windowSoftInputMode="adjustPan"
┼── 2024-10-10 08:34:10

@manifest 在您的活动中:

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

@manifest in your activity:

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
静谧幽蓝 2024-10-10 08:34:10

就我而言 - 软键盘向上移动背景图像 - 这里发布的答案都不起作用。不过我找到了解决方案。

设置整个屏幕(活动)的背景图像:

  • 使用您的背景创建样式:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/background_image</item>
</style>
  • 在活动上应用主题:
<activity
    android:name=".Activity"
    android:theme="@style/AppTheme">

最后,背景图像将保持固定在适当的位置

In my case - soft keyboard moving up the BACKGROUND image - none of the answers published here worked. However I found a solution.

Set the background image for the whole screen (activity):

  • Create a style with your background:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/background_image</item>
</style>
  • Apply the theme on the activity:
<activity
    android:name=".Activity"
    android:theme="@style/AppTheme">

In the end, the background image will remain fixed in place

Smile简单爱 2024-10-10 08:34:10

我使用的是透明状态栏,所以对我来说没有任何作用。隐藏状态栏不是透明的状态栏,而是对我有用

window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

,并将其添加到清单中的活动

android:windowSoftInputMode="adjustResize"

对我有用

I was using transparent status bar so nothing worked for me. Instead of transparent statusbar hiding the status bar worked for me

window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

and added this for activity in manifest

android:windowSoftInputMode="adjustResize"

Worked for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文