java Android中键盘和EditText之间的空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17630336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:31:29  来源:igfitidea点击:

Space between keyboard and EditText in Android

javaandroidkeyboard

提问by altosh

Soft input mode is "SOFT_INPUT_ADJUST_PAN", and when keyboard shown, EditTextmoves up to stay visible, but the keyboard and txt's bottom are always sticked together as seen on screenshot, I want some space between them, is it possible?

软输入模式是"SOFT_INPUT_ADJUST_PAN",当键盘显示时,EditText向上移动以保持可见,但键盘和 txt 的底部总是粘在一起,如屏幕截图所示,我希望它们之间有一些空间,这可能吗?

Also input mode must stay as "SOFT_INPUT_ADJUST_PAN".

输入模式也必须保持为"SOFT_INPUT_ADJUST_PAN".

Sorry form my english, Thanks in Advance...

对不起,我的英语,提前致谢...

Screenshot:

截屏:

回答by Dhiraj Himani

I applied above methods which worked for me was Set android:windowSoftInputMode on the Activity to "adjustPan" than i added the padding to the edittext with gravity bottom and it works like charm. Thanks,

我应用了上述对我有用的方法是将 Activity 上的 android:windowSoftInputMode 设置为“adjustPan”,而不是我将填充添加到带有重力底部的编辑文本中,它的工作原理就像魅力一样。谢谢,

回答by Bhavin Nattar

May this help you:

可以帮助你:

If this line is written in your code then remove it:

如果此行写在您的代码中,则将其删除:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

OR:

或者:

You should not be setting your layout_widthand layout_heightswith explicit pixel values. Instead, use wrap_parentor fill_parentas appropriate.

您不应该使用明确的像素值设置您的layout_widthlayout_heights。而是根据需要使用wrap_parentfill_parent

OR:

或者:

If you have set your minSdkVersionto 3then change it to minSdkVersion=4

如果您已设置minSdkVersion为,3则将其更改为minSdkVersion=4

Edit:

编辑:

Set android:windowSoftInputModeon the Activity to "adjustPan"

android:windowSoftInputMode在 Activity 上设置为"adjustPan"

OR:

或者:

Add this code in your main activity in setOnTouchListener()of your EditText:

在主要活动添加该代码在setOnTouchListener()您的EditText

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);

回答by Guy

Why don't you try this?

你为什么不试试这个?

android:windowSoftInputMode="adjustResize"

I know you want to adjust pan but this would solve your problem aswell.

我知道您想调整平底锅,但这也可以解决您的问题。

Since you really want to stay with adjustPan, maybe you can try re-designing the XML layout a little bit.

既然你真的想继续使用 adjustPan,也许你可以尝试重新设计一下 XML 布局。

You must also add your required padding to the outer most container element in the layout file.

您还必须将所需的填充添加到布局文件中最外层的容器元素。

回答by Chao

Just wondering, is your app full screen? does it have the status bar? If it is try to disable full screen mode and try to see if that solves your problem. I remember there was a bug about this some time ago.

只是想知道,你的应用程序是全屏的吗?有状态栏吗?如果尝试禁用全屏模式并尝试查看是否可以解决您的问题。我记得前段时间有一个关于这个的错误。

回答by Serg Burlaka

try to useGlobalLayoutListener:

尝试使用GlobalLayoutListener:

private var focusView:View?=null

私有 var focusView:View?=null

// Register global layout listener.
    decorView?.viewTreeObserver?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {

        private val windowVisibleDisplayFrame = Rect()
        private var lastVisibleDecorViewHeight: Int = 0

        override fun onGlobalLayout() {

            // Retrieve visible rectangle inside window.
            decorView.getWindowVisibleDisplayFrame(windowVisibleDisplayFrame)
            val visibleDecorViewHeight = windowVisibleDisplayFrame.height()

            // Decide whether keyboard is visible from changing decor view height.
            if (lastVisibleDecorViewHeight != 0) {
                if (lastVisibleDecorViewHeight > visibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX) {
                    // Calculate current keyboard height (this includes also navigation bar height when in fullscreen mode).
                    val currentKeyboardHeight = decorView.height - windowVisibleDisplayFrame.bottom

        //keyboardHeight = currentKeyboardHeight


          focusView =  activity?.currentFocus     
          focusView.setPadding(0, 0, 0, SET_BOTTOM_PADDING_SPACE)  // <---set space here



                } else if (lastVisibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX < visibleDecorViewHeight) {

                    focusView.setPadding(0, 0, 0, 0)

                }
            }
            // Save current decor view height for the next call.
            lastVisibleDecorViewHeight = visibleDecorViewHeight
        }
    })

回答by Vishambar Pandey

Simple padding in edit text won't work.

编辑文本中的简单填充不起作用。

Use drawable with your edit text like below:

将 drawable 与您的编辑文本一起使用,如下所示:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="8dp"
        android:left="8dp"
        android:right="8dp"
        android:top="8dp">

        <shape android:shape="rectangle">

            <corners android:radius="10dp" />
            <stroke
                android:width="1dp"
                android:color="@color/white" />
            <solid android:color="@color/transparent" />
        </shape>
    </item>
</layer-list>

and then use

然后使用

    android:windowSoftInputMode="adjustPan"

in your manifest file.

在您的清单文件中。