软键盘将其上方的按钮向上推。如何解决?
Soft Keyboard pushed button up above it. How to fix it?
我有 ConstraintLayout,它下面有 ScorllView 和 Button(连接到屏幕底部。当我在 ScrollView 中编辑 EditText 输入时。然后出现的键盘正在向上移动我的 ScrollView 内容(所需的行为,所以我可以滚动到它结束了),但它也按下了按钮(不希望的行为)。
我想我可以更改 windowAdjustMode,也许我可以检测到键盘显示然后隐藏此按钮?但这两种解决方案并不完美。
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/submitButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="0dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText /> goes here
</android.support.constraint.ConstraintLayout>
</ScrollView>
<Button
android:id="@+id/submitButton"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="0dp"
android:text="@string/wizard_singup_step_submit_button"
style="@style/FormSubmitButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
这可能会有所帮助,我自己还没有尝试过,请尝试将以下代码添加到清单
内的 activity
标记中
编辑 - 已添加 stateHidden
以实现您正在寻找的内容,按钮将位于底部并且可以滚动滚动视图中的元素。
android:windowSoftInputMode="adjustPan|stateHidden"
来自 Android Documentation -
adjustPan
- activity 的主要 window 未调整大小以为软键盘腾出空间。相反,window 的内容会自动平移,以便当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入的内容。这通常不如调整大小可取,因为用户可能需要关闭软键盘才能找到 window.
的遮挡部分并与之交互
编辑 2 - 计算键盘高度的代码
myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
parent.getWindowVisibleDisplayFrame(r);
int screenHeight = parent.getRootView().getHeight();
int heightDifference = screenHeight - (r.bottom - r.top);
Log.d("Keyboard Size", "Size: " + heightDifference);
}
});
通过以编程方式创建视图并设置其高度来添加 heightDifference
。
编辑 3 -
用它来隐藏键盘
public static void hideKeyboardFrom(Context context, View view) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
如果可行,请告诉我。
我有 ConstraintLayout,它下面有 ScorllView 和 Button(连接到屏幕底部。当我在 ScrollView 中编辑 EditText 输入时。然后出现的键盘正在向上移动我的 ScrollView 内容(所需的行为,所以我可以滚动到它结束了),但它也按下了按钮(不希望的行为)。
我想我可以更改 windowAdjustMode,也许我可以检测到键盘显示然后隐藏此按钮?但这两种解决方案并不完美。
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/submitButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="0dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText /> goes here
</android.support.constraint.ConstraintLayout>
</ScrollView>
<Button
android:id="@+id/submitButton"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="0dp"
android:text="@string/wizard_singup_step_submit_button"
style="@style/FormSubmitButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
这可能会有所帮助,我自己还没有尝试过,请尝试将以下代码添加到清单
内的activity
标记中
编辑 - 已添加 stateHidden
以实现您正在寻找的内容,按钮将位于底部并且可以滚动滚动视图中的元素。
android:windowSoftInputMode="adjustPan|stateHidden"
来自 Android Documentation -
adjustPan
- activity 的主要 window 未调整大小以为软键盘腾出空间。相反,window 的内容会自动平移,以便当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入的内容。这通常不如调整大小可取,因为用户可能需要关闭软键盘才能找到 window.
编辑 2 - 计算键盘高度的代码
myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
parent.getWindowVisibleDisplayFrame(r);
int screenHeight = parent.getRootView().getHeight();
int heightDifference = screenHeight - (r.bottom - r.top);
Log.d("Keyboard Size", "Size: " + heightDifference);
}
});
通过以编程方式创建视图并设置其高度来添加 heightDifference
。
编辑 3 -
用它来隐藏键盘
public static void hideKeyboardFrom(Context context, View view) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
如果可行,请告诉我。