简体   繁体   English

显示键盘时以固定高度调整 EditText 的大小

[英]Resize EditText with fixed height when keyboard is shown

I have a screen with a small text on top, an editText under it and a button on the bottom of the screen.我有一个屏幕,上面有一个小文本,下面有一个 editText,屏幕底部有一个按钮。 The editText view should have a given height (around 30% of screen size), but when I open the keyboard, the button will be pushed up (as it should) and overlaps with the editText. editText 视图应该有一个给定的高度(大约 30% 的屏幕大小),但是当我打开键盘时,按钮将被向上推(应该如此)并与 editText 重叠。 How can I make the editText resize itself when the keyboard shows up, so that it won't overlap with the button?当键盘出现时,如何使 editText 自行调整大小,使其不会与按钮重叠? I tried giving it a min & max height, but that didn't affect it, probably because of the fixed height.我试着给它一个最小和最大高度,但这并没有影响它,可能是因为固定高度。

My code:我的代码:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="text"
        android:layout_marginStart="20dp"
        android:textAppearance="@style/TextAppearance.AppTheme.Job.Subtitle"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="300dp" 
        android:windowSoftInputMode="stateVisible|adjustNothing"
        android:adjustViewBounds="true"
        android:minHeight="150dp"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp"
        android:padding="5dp"
        android:gravity="top"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text"
        app:layout_constraintVertical_bias="0" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="56dp"
        android:layout_marginEnd="56dp"
        android:layout_marginBottom="14dp"
        tools:visibility="visible"
        android:backgroundTint="@color/button_color"
        android:enabled="false"
        android:text="@string/content_feedback_button_text"
        android:textColor="@color/white"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:tint="@color/white" />

Delete these attributes from the EditText :EditText中删除这些属性:

android:layout_height="300dp" 
android:adjustViewBounds="true"
android:minHeight="150dp"

Then, add these:然后,添加这些:

android:layout_height="0dp"
app:layout_constraintHeight_min="150dp"
app:layout_constraintHeight_max="300dp"

This will make sure that your EditText (shown here with a white background) consumes 300dp when there's a lot of space:这将确保您的EditText (此处显示为白色背景)在空间很大时消耗 300dp:

在此处输入图像描述

And shrinks (down to a minimum of 150dp) when there's less space:并在空间较小时缩小(最小为 150dp):

在此处输入图像描述

Maybe you find an answer here:也许你会在这里找到答案:
How can I change the height of a EditText and a Button? 如何更改 EditText 和 Button 的高度?

There is mentioned to use "android:layout_height" instead of "android:heigt".提到使用“android:layout_height”而不是“android:heigt”。
Also pay attention, not to use "wrap_content" and don't decrease size of TextEdit if the font is too big inside.还要注意,不要使用“wrap_content”,如果里面的字体太大,也不要减小 TextEdit 的大小。

That are a few hints, why this may not work.这是一些提示,为什么这可能不起作用。

Plus, how do you check the visibility of your keyboard?另外,您如何检查键盘的可见性? In case, you don't have a solution so far, this links can help:如果您目前还没有解决方案,此链接可以提供帮助:
How do I detect if software keyboard is visible on Android Device or not? 如何检测软件键盘在 Android 设备上是否可见?
How to check visibility of software keyboard in Android? 如何检查 Android 中软件键盘的可见性?

Maybe in newer SDK there are simpler methods, but to refer to the link, where it is handled over the diff in the screen:也许在较新的 SDK 中有更简单的方法,但要参考链接,它是通过屏幕上的差异处理的:

final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
        if (heightDiff > dpToPx(this, 200)) { // if more than 200 dp, it's probably a keyboard...
            // ... do something here
        }
    }
});

And then use it like:然后像这样使用它:

public static float dpToPx(Context context, float valueInDp) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
}

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

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