简体   繁体   English

Android中键盘和EditText之间的空格

[英]Space between keyboard and EditText in Android

Soft input mode is "SOFT_INPUT_ADJUST_PAN" , and when keyboard shown, EditText moves 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... 抱歉,我的英文,在此先感谢...

截图:

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. 您还必须将所需的填充添加到布局文件中最外层的容器元素。

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_width and layout_heights with explicit pixel values. 您不应该使用显式像素值设置layout_widthlayout_heights Instead, use wrap_parent or fill_parent as appropriate. 相反,请根据需要使用wrap_parentfill_parent

OR: 要么:

If you have set your minSdkVersion to 3 then change it to minSdkVersion=4 如果已将minSdkVersion设置为3则将其更改为minSdkVersion=4

Edit: 编辑:

Set android:windowSoftInputMode on the Activity to "adjustPan" 将Activity上的android:windowSoftInputMode设置为"adjustPan"

OR: 要么:

Add this code in your main activity in setOnTouchListener() of your EditText : EditText setOnTouchListener()中的主活动中添加此代码:

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

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. 我应用上面的方法,对我来说是有效的设置android:windowSoftInputMode活动到“adjustPan”比我添加填充到edittext与重力底部,它的工作就像魅力。 Thanks, 谢谢,

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. 我记得前段时间有一个关于这个的错误。

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. 在您的清单文件中。

try to use GlobalLayoutListener: 尝试使用 GlobalLayoutListener:

private var focusView:View?=null private 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
        }
    })

If you need to have a distance between the keyboard and the EditText , this can be done easily by just add padding bottom to the EditText : 如果您需要在键盘EditText之间保持一定距离,只需在EditText添加填充底部即可轻松完成:

 <EditText
     android:id="@+id/your_edit_text"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:paddingBottom="10dp"/> 

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

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