5

我在我的应用程序中使用时遇到了一些问题adjustPan,因为它没有按预期工作。其他一些用户也遇到了这个问题,所以我认为分享这个问题很重要(点击 EditText 时布局没有向上移动足够)。

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

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

不仅如此,最奇怪的是状态栏变得透明,并且应该在通知图标后面的深蓝色栏下降了一点(详见图片)。

问题截图

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

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

这是我的一些代码:

清单文件:

<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" />

所有反馈将不胜感激。

4

4 回答 4

3

在类级别创建一个像 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
替换它 希望它有帮助。

于 2019-02-09T06:06:08.927 回答
0

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

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

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

在你的 android 清单中。 在这里阅读

希望这可以帮助!

于 2019-02-09T06:20:10.423 回答
0
android:windowSoftInputMode

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

下图更能解你的疑惑在此处输入图像描述

  • adjustPan : Activity 的主窗口没有调整大小来为软键盘腾出空间。相反,窗口的内容会自动平移,因此当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入的内容。这通常不如调整大小可取,因为用户可能需要关闭软键盘才能到达窗口的模糊部分并与之交互。
  • adjustResize : Activity 的主窗口总是调整大小,以便为屏幕上的软键盘腾出空间。

我认为不需要添加您添加的以下代码。

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

于 2019-02-15T10:02:47.177 回答
0

还需要在您的 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;
        }
    });
于 2019-02-06T01:59:13.653 回答