【问题标题】:Why doesn't my layout move up enough with "adjustPan"?为什么我的布局没有用“adjustPan”向上移动?
【发布时间】:2019-06-29 22:19:09
【问题描述】:

我在我的应用程序中使用 adjustPan 时遇到了一些问题,因为它无法正常工作。其他一些用户也遇到了这个问题,所以我认为分享这个问题很重要(Layout doesn't move up enough when clicking EditText)。

注意:我使用的是 Android Studio并且我使用的是“Instant App”,因此文件的位置会有所不同。

当我在布局中使用adjustPan 时,我的活动没有向上移动到EditText 出现在软键盘上方的位置(详见图片)。无论您在 Manifest 文件或 Java 文件中使用它,仍然会出现相同的结果。

不仅如此,最奇怪的是状态栏变得透明,而应该在通知图标后面的深蓝色栏向下稍微向下 (详见图片)。

我已尝试多次尝试解决此问题,例如使用adjustResizesetOnTouchListener,但我收到的这两个答案都没有解决此问题。

我仍然无法弄清楚这个问题的原因,所以任何关于如何发现这个问题发生原因的答案或 cmet 也很好。

这是我的一些代码:

清单文件:

<activity
        android:name=".Input_Activity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />

Java 文件:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;

public class Input_Activity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input__activity);

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

XML 文件:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"

android:layout_height="match_parent"
tools:context=".Input_Activity">

<EditText
    android:layout_width="214dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:inputType="number|numberDecimal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.585"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.725" />

所有反馈将不胜感激。

【问题讨论】:

    标签: android android-edittext android-statusbar window-soft-input-mode adjustpan


    【解决方案1】:

    在类级别创建一个像 InputMethodManager methodManager 这样的对象。
    将这些行添加到您的 onCreate() 方法中,

    methodManager = (InputMethodManager) this.getSystemService(this.INPUT_METHOD_SERVICE);
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    

    在你的 onClick() 方法中,显示键盘,

    methodManager.toggleSoftInputFromWindow(
        your_view_name.getApplicationWindowToken(),
        InputMethodManager.SHOW_FORCED, 0);  
    

    如果这个词 getApplicationWindowToken 引发错误,则将其替换为 getWindowToken
    希望有帮助。

    【讨论】:

    • 我对 Android Studio 有点陌生,应该怎么做:your_view_name
    • 即使这不是adjustPan,它仍然可以正常工作。如果您能够使用 adjustPan 解决此问题,那就太好了
    【解决方案2】:

    还需要在你的 onTouch 事件中添加以下代码,以便当软键盘出现时它会向上。示例:

     editText.setTransformationMethod(WordBreakUtil.getInstance());
     editText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
                return false;
            }
        });
    

    【讨论】:

    • Android Studio(我的 IDE)指出:“它无法解析符号 WordBreakUtil
    【解决方案3】:

    我不确定答案。但尝试替换

    <activity
        android:name=".Input_Activity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    

    <activity
        android:name=".Input_Activity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize" />
    

    在您的 android 清单中。 Read here

    希望这会有所帮助!

    【讨论】:

      【解决方案4】:
      android:windowSoftInputMode
      

      Above 属性可用于指定活动内容发生的情况。

      下图更能解你的疑惑

      • adjustPan:活动的主窗口未调整大小以腾出空间 软键盘。相反,窗口的内容是 自动平移,使当前焦点永远不会被 键盘和用户始终可以看到他们正在输入的内容。这是 通常不如调整大小,因为用户可能需要 关闭软键盘以获取并与 窗户。
      • adjustResize:活动的主窗口总是调整大小以使 屏幕上的软键盘空间。

      我认为没有必要在下面添加您添加的代码。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-22
        • 2017-04-10
        • 1970-01-01
        • 2018-02-03
        相关资源
        最近更新 更多