简体   繁体   English

显示软键盘时向上移动布局?

[英]Move layouts up when soft keyboard is shown?

I have a few elements in a RelativeView with the align bottom attribute set, when the soft keyboard comes up the elements are hidden by the soft keyboard.我在相对视图中有几个元素,设置了对齐底部属性,当软键盘出现时,这些元素被软键盘隐藏。

I would like them to move up so that if there is enough screen space they are shown above the keyboard, or to make the section above the keyboard scrollable so the user can still see the elements.我希望它们向上移动,以便如果有足够的屏幕空间,它们会显示在键盘上方,或者使键盘上方的部分可滚动,以便用户仍然可以看到元素。

Any ideas on how to approach this?关于如何解决这个问题的任何想法?

Yes, check out this article on the Android developers' site which describes how the framework handles the soft keyboard appearing.是的,请查看Android 开发人员站点上的这篇文章,该文章描述了框架如何处理出现的软键盘。

The android:windowSoftInputMode attribute can be used to specify what happens on a per-activity basis: whether the layout is resized or whether it scrolls etc. android:windowSoftInputMode属性可用于指定在每个活动的基础上发生的事情:布局是否调整大小或是否滚动等。

您还可以在onCreate()方法中使用此代码:

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

为您的活动添加 AndroidManifest.xml:

android:windowSoftInputMode="adjustPan|adjustResize"

Make changes in the activity of your Manifest file like在您的清单文件的活动中进行更改,例如

android:windowSoftInputMode="adjustResize"

OR或者

Make changes in your onCreate() method in the activity class like在活动类中的onCreate()方法中进行更改,例如

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

similar issue i was facing with my layout which consist scroll view inside.我的布局遇到了类似的问题,其中包含滚动视图。 Whenever i try to select text in EditText,Copy/Cut/Paste action bar gets moved up with below code as such it does not resize layout每当我尝试在 EditText 中选择文本时,复制/剪切/粘贴操作栏会随着下面的代码向上移动,因此它不会调整布局大小

android:windowSoftInputMode="adjustPan"

By modifying it to as below通过将其修改为如下

  1. AndroidManifest.xml AndroidManifest.xml

    android:windowSoftInputMode="stateVisible|adjustResize" android:windowSoftInputMode="stateVisible|adjustResize"

  2. style.xml file ,in activity style style.xml 文件,活动样式

    true真的

It worked for me.它对我有用。

In AndroidManifest.xml, don't forget to set:在 AndroidManifest.xml 中,不要忘记设置:

 android:windowSoftInputMode="adjustResize"

and for RelativeLayout inside ScrollView ,set :对于 ScrollView 内的 RelativeLayout ,设置:

 android:layout_gravity="center" or android:layout_gravity="bottom" 

it will be okay会好起来的

for Activity in its oncreate methode insert below line对于 Activity 在其 oncreate 方法中插入下面的行

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

and for fragments in its onCreate method以及 onCreate 方法中的片段

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

There are 3 steps to follow to scroll screen up.向上滚动屏幕有 3 个步骤。

  1. Write in manifest file for your activity:为您的活动写入清单文件:

android:windowSoftInputMode="adjustResize" android:windowSoftInputMode="adjustResize"

  1. Add following line in oncreate() method of your activity在您的活动的 oncreate() 方法中添加以下行

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

  1. Then place your whole view in "Scrollview" in XML file like :然后将整个视图放在 XML 文件中的“Scrollview”中,例如:
 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:background="@drawable/bg" android:layout_height="match_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_gravity="center" > <TextView /> ......... <TextView /> <EditText > <requestFocus /> </EditText> <Button /> </RelativeLayout> </ScrollView>

PS Don't forget to add android:layout_gravity="center" in parent Relative layout. PS 不要忘记在父相对布局中添加 android:layout_gravity="center"。

<activity name="ActivityName"
    android:windowSoftInputMode="adjustResize">
    ...
</activity>

Add NestedScrollView around layout which you want to scroll, this will perfectly work在要滚动的布局周围添加 NestedScrollView,这将完美地工作

I tried a method of Diego Ramírez, it works.我尝试了 Diego Ramírez 的一种方法,它有效。 In AndroidManifest:在 AndroidManifest 中:

    <activity
        android:name=".MainActivity"
        android:windowSoftInputMode="stateHidden|adjustResize">
        ...
    </activity>

In activity_main.xml:在activity_main.xml 中:

<LinearLayout ...
    android:orientation="vertical">

<Space
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1" />

<EditText
    android:id="@+id/edName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/first_name"
    android:inputType="textPersonName" />

<EditText ...>
...
</LinearLayout>

I have found a solution to my problems that it's pretty same as yours.我已经找到了解决我的问题的方法,它与您的问题非常相似。

First of all, I only have a LinearLayout with 2 elements, an ImageView and a LinearLayout wich contains 2 EditText and one Button, so I needed to keep them separate in full screen without keyboard, and when the keyboard appears they should look closer.首先,我只有一个包含 2 个元素的 LinearLayout,一个 ImageView 和一个包含 2 个 EditText 和一个 Button 的 LinearLayout,所以我需要在没有键盘的情况下在全屏模式下将它们分开,当键盘出现时,它们应该看起来更近一些。

So I added the view between them with the height attribute in 0dp and weight 1, that let me keep my views separate one form each other.所以我在它们之间添加了高度属性为 0dp 和重量为 1 的视图,这让我让我的视图彼此分开。 and when the keyboard appers, as they don't have a fix height, they just resize and keep the aspect.当键盘出现时,由于它们没有固定的高度,它们只是调整大小并保持外观。

Voila!瞧! The layout resize and the distance between my views is the same when the keyboard is or not present.当键盘存在或不存在时,布局调整大小和视图之间的距离是相同的。

  1. If user want this task using manifest then add this line in manifest如果用户希望使用清单执行此任务,则在清单中添加此行
  2. In manifest add this line: - android:windowSoftInputMode="adjustPan" eg在清单中添加这一行: - android:windowSoftInputMode="adjustPan" 例如

In Java File or Kotlin File (Programmatically):- If the user wants this using Activity then add the below line in onCreate Method在 Java 文件或 Kotlin 文件中(以编程方式):- 如果用户希望使用 Activity,则在 onCreate 方法中添加以下行

activity?.window()?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

Here is full manifest code这是完整的清单代码

<activity
        android:name=".activities.MainActivity"
        android:windowSoftInputMode="adjustResize"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

In your manifest file add the below line.在您的清单文件中添加以下行。 It will work它会工作

<activity name="MainActivity"
    android:windowSoftInputMode="stateVisible|adjustResize">
    ...
</activity>

我刚刚在顶层布局中添加了以下行,一切正常:

android:fitsSystemWindows="true"

如果您处于片段中,那么您必须将以下代码添加到您的活动的 onCreate 中,它为我解决了问题

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

Alright this is very late however I've discovered that if you add a List View under your edit text then the keyboard will move all layouts under that edittext without moving the ListView好吧,这已经很晚了,但是我发现如果您在编辑文本下添加列表视图,那么键盘将移动该编辑文本下的所有布局而不移动 ListView

<EditText
android:id="@+id/et"
android:layout_height="match_parent"
android:layout_width="match_parent"



/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"//can make as 1dp
    android:layout_below="@+id/et" // set to below editext
    android:id="@+id/view"
    >
**<any layout**

This is the only solution that worked for me.这是唯一对我有用的解决方案。

在此输入图像描述

Here is the solution for this fix
- android:windowSoftInputMode="adjustResize" in Manifest File
- Make a class "CommentKeyBoardFix.Java" and copy and paste the below code.

public class CommentKeyBoardFix
{
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private Rect contentAreaOfWindowBounds = new Rect();

public CommentKeyBoardFix(Activity activity)
{
    FrameLayout content = activity.findViewById(android.R.id.content);
    mChildOfContent = content.getChildAt(0);
    mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent);
    frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}

private void possiblyResizeChildOfContent()
{
    int usableHeightNow = computeUsableHeight();
    if (usableHeightNow != usableHeightPrevious)
    {
        int heightDifference = 0;
        if (heightDifference > (usableHeightNow /4))
        {
            // keyboard probably just became visible
            frameLayoutParams.height = usableHeightNow - heightDifference;
        }
        else
        {
            // keyboard probably just became hidden
            frameLayoutParams.height = usableHeightNow;
        }
        mChildOfContent.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom);
        mChildOfContent.requestLayout();
        usableHeightPrevious = usableHeightNow;
    }
}

private int computeUsableHeight()
{
    mChildOfContent.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds);
    return contentAreaOfWindowBounds.height();
}
}

And then call this class in your Activity or Fragment
setContentView(R.layout.your_comments_layout)
CommentKeyBoardFix(this) ---> For Kotlin
or
new CommentKeyBoardFix(this) ---> For Java

Not a single advice solve my issue.没有一个建议可以解决我的问题。 Adjust Pan is almost as like work Adjust Resize . Adjust Pan几乎就像工作Adjust Resize If i set Adjust Nothing than layout not move up but it hide my edittext of bottom that i am corrently writing.如果我设置Adjust Nothing 而不是布局不会向上移动,但它会隐藏我正在编写的底部的编辑文本。 So after 24 hour of continuous searching I found this source and it solve my problem.所以经过 24 小时的连续搜索,我找到了这个来源,它解决了我的问题。

It might be late but I want to add few things.可能会迟到,但我想补充一些东西。 In Android Studio Version 4.0.1, and Gradle Version 6.1.1, the windowSoftInputMode will not work as expected if you are adding the following flags in your Activity :在 Android Studio 4.0.1 版和 Gradle 6.1.1 版中,如果您在 Activity 中添加以下标志, windowSoftInputMode将不会按预期工作:

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

After wasting lot of time, I have found that hack.在浪费了很多时间之后,我发现了那个黑客。 In order for adjustResize or AdjustPan or any attribute of windowSoftInputMode to work properly, you need to remove that from your activity.为了adjustResizeAdjustPan或任何属性windowSoftInputMode正常工作,您需要从活动移除。

Use Scroll View as a Parent and use this Tag in Scroll View使用滚动视图作为父级并在滚动视图中使用此标签

android:fillViewport="true"机器人:fillViewport =“真”

This is what worked for me after looking at a lot of solutions:在查看了很多解决方案后,这对我有用:

AndroidManifest.xml

Add 

    android:windowSoftInputMode="stateHidden|adjustPan" 

to an activity declaration in manifest:

    <activity
        android:name=".MainActivity"
        android:screenOrientation="landscape"
        android:windowSoftInputMode="stateHidden|adjustPan">
    </activity>

Add this Line to the view将此行添加到视图

view.requestFocus(); view.requestFocus();

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

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