带键盘向上移动编辑文本的底表 [英] Bottomsheet with edit text moved up by keyboard

查看:54
本文介绍了带键盘向上移动编辑文本的底表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个底片片段,显示给用户的是他的评论.在底部工作表的底部,我们有编辑文本,用户可以在其中添加新注释.因此,当软键盘打开时,键盘上方的底页保持静止,并且其顶部远远超出屏幕.但是打开键盘时,底页应该调整大小.

I have bottomsheet fragment what shows to user answears to his comment. At the bottom of bottom sheet we have edit text, where user can add new comment. So, when soft keyboard opened the bottomsheet staes above keyboard and its top moved far beyond the screen. But bottom sheet should to resize when keyboard is open.

这是我的代码:

<android.support.design.widget.CoordinatorLayout
    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">

    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include android:id="@+id/toolbar_layout"
                 layout="@layout/partial_toolbar" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/list_comments"
            android:layout_below="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:overScrollMode="always"
            android:layout_height="match_parent"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_above="@+id/comment_container"
            android:layout_alignBaseline="@+id/list_comments"
            android:background="@drawable/elevation_bottom"/>


        <LinearLayout
            android:id="@+id/comment_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ebffffff"
            android:layout_alignParentBottom="true"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingBottom="@dimen/vertical_spacing"
            android:paddingEnd="@dimen/horizontal_spacing"
            android:paddingStart="8dp"
            android:paddingTop="@dimen/vertical_spacing">

            <android.support.v7.widget.AppCompatImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/transparent"
                android:src="@drawable/ic_add_blue_small"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:background="@drawable/bg_message_field"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <android.support.v7.widget.AppCompatEditText
                    android:id="@+id/edit_comment"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@color/transparent"
                    android:hint="@string/comments_add_comment_hint"
                    android:inputType="textMultiLine"
                    android:maxLength="200"
                    android:maxLines="3"
                    android:minLines="1"
                    android:paddingBottom="8dp"
                    android:paddingEnd="0dp"
                    android:paddingStart="8dp"
                    android:paddingTop="8dp"
                    android:textSize="14sp"
                    app:fontFamily="@font/lora_regular"/>

                <android.support.v7.widget.AppCompatImageButton
                    android:id="@+id/button_send"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:background="@color/transparent"
                    app:srcCompat="@drawable/ic_send_message"/>

            </LinearLayout>

        </LinearLayout>

    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

清单上的活动(此活动将打开对话框)

Activity at manifest (this activity opens dialog)

    <activity
 android:name=".presentation.ui.category.CategoryDetailsActivity"
android:configChanges="locale|orientation|screenSize|keyboard"
                android:windowSoftInputMode="adjustResize"
                android:windowTranslucentNavigation="true"
                android:windowTranslucentStatus="true"
                android:launchMode="singleTop"
                android:screenOrientation="portrait"
                android:theme="@style/AppTheme.Main.NoActionBar" />

我也将此代码放入方法setupDialog

Also I put this code to method setupDialog

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

和屏幕截图

推荐答案

在我的一个项目中我也遇到了同样的问题,

As I too had the same issue in one of my projects,

BottomSheetFragment

new KeyboardUtil(getActivity(), rootView);

通过使用下面的类

public class KeyboardUtil {
    private View decorView;
    private View contentView;
    //a small helper to allow showing the editText focus
    ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            //r will be populated with the coordinates of your view that area still visible.
            decorView.getWindowVisibleDisplayFrame(r);

            //get screen height and calculate the difference with the useable area from the r
            int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
            int diff = height - r.bottom;

            //if it could be a keyboard add the padding to the view
            if (diff != 0) {
                // if the use-able screen height differs from the total screen height we assume that it shows a keyboard now
                //check if the padding is 0 (if yes set the padding for the keyboard)
                if (contentView.getPaddingBottom() != diff) {
                    //set the padding of the contentView for the keyboard
                    contentView.setPadding(0, 0, 0, diff);
                }
            } else {
                //check if the padding is != 0 (if yes reset the padding)
                if (contentView.getPaddingBottom() != 0) {
                    //reset the padding of the contentView
                    contentView.setPadding(0, 0, 0, 0);
                }
            }
        }
    };

    public KeyboardUtil(Activity act, View contentView) {
        this.decorView = act.getWindow().getDecorView();
        this.contentView = contentView;

        //only required on newer android versions. it was working on API level 19
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    /**
     * Helper to hide the keyboard
     *
     * @param act
     */
    public static void hideKeyboard(Activity act) {
        if (act != null && act.getCurrentFocus() != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), 0);
        }
    }

    public void enable() {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    public void disable() {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }
}

这篇关于带键盘向上移动编辑文本的底表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆