关于软键盘将edittext顶上去的问题

最近有个这样的需求,说点击每一个edittext软件盘弹起的时候都不能挡住所有的输入布局,需要往上顶,但是头部的标题栏又不能动,如下图所示点击name的输入框时软键盘弹起,但是不能将phone一栏遮盖,且最上层的头部不能动找了好多资料都会把头部给顶上去,最后想到了一个办法,将标题栏下面的内容的最外层套上ScrollView,然后监听软键盘弹起的高度,动态设置phone一栏距离底部的高度。实现了这个需求,代码如下
1、布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground"
    android:orientation="vertical"
    android:fitsSystemWindows="true">

    <include layout="@layout/layout_title_bar"
        android:id="@+id/tv_titleBar"
        />
    <ScrollView
        android:id="@+id/sc_scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:scrollbars="none"
        android:layout_below="@+id/tv_titleBar">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:minHeight="150dp"
                android:orientation="vertical"
                android:paddingTop="10dp"
                android:paddingBottom="10dp">

                <com.auratech.base.common.widget.CircleImageView
                    android:id="@+id/iv_headView"
                    android:layout_width="90dp"
                    android:layout_height="90dp"
                    android:src="@drawable/add_contact_head_icon" />
            </LinearLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/line_bg_color" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/c11"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:gravity="center_vertical"
                android:minHeight="60dp"
                android:orientation="horizontal"
                android:paddingLeft="15dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Name :"
                    android:textColor="@color/color_333333"
                    android:textSize="@dimen/text_size32" />

                <EditText
                    android:id="@+id/et_name"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@null"
                    android:gravity="center_vertical"
                    android:inputType="text"
                    android:maxLength="20"
                    android:singleLine="true"
                    android:textColor="@color/colorLoadingText"
                    android:textCursorDrawable="@drawable/edit_cursor_bg"
                    android:textSize="16dp" />
            </LinearLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="15dp"
                android:background="@color/line_bg_color" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/c11"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:gravity="center_vertical"
                android:minHeight="60dp"
                android:orientation="horizontal"
                android:paddingLeft="15dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Note :"
                    android:textColor="@color/color_333333"
                    android:textSize="@dimen/text_size32" />

                <EditText
                    android:id="@+id/et_note"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@null"
                    android:gravity="center_vertical"
                    android:singleLine="true"
                    android:textColor="@color/colorLoadingText"
                    android:textCursorDrawable="@drawable/edit_cursor_bg"
                    android:textSize="16dp" />
            </LinearLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="15dp"
                android:background="@color/line_bg_color" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFFFFF"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:gravity="center_vertical"
                android:minHeight="60dp"
                android:orientation="horizontal"
                android:paddingLeft="15dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Phone :"
                    android:textColor="@color/colorLoadingText"
                    android:textSize="16dp" />

                <EditText
                    android:id="@+id/et_phone"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@null"
                    android:gravity="center_vertical"
                    android:inputType="phone"
                    android:maxLength="20"
                    android:singleLine="true"
                    android:textColor="@color/colorLoadingText"
                    android:textCursorDrawable="@drawable/edit_cursor_bg"
                    android:textSize="16dp" />
            </LinearLayout>

            <TextView
                android:id="@+id/tv_phone_line"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="15dp"
                android:background="@color/line_bg_color" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

2、自定义软键盘的监听:

package com.auratech.contacts.ui.activity;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * <pre>
 *     author : Ben
 *     e-mail : ben-lau@auratech.hk
 *     time   : 2019/03/22
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class SoftKeyBoardListener {
    private View rootView;//activity的根视图
    int rootViewVisibleHeight;//纪录根视图的显示高度
    private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;

    public SoftKeyBoardListener(Activity activity) {
        //获取activity的根视图
        rootView = activity.getWindow().getDecorView();

        //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //获取当前根视图在屏幕上显示的大小
                Rect r = new Rect();
                rootView.getWindowVisibleDisplayFrame(r);

                int visibleHeight = r.height();
                System.out.println(""+visibleHeight);
                if (rootViewVisibleHeight == 0) {
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                //根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
                if (rootViewVisibleHeight == visibleHeight) {
                    return;
                }

                //根视图显示高度变小超过200,可以看作软键盘显示了
                if (rootViewVisibleHeight - visibleHeight > 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                //根视图显示高度变大超过200,可以看作软键盘隐藏了
                if (visibleHeight - rootViewVisibleHeight > 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

            }
        });
    }

    private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
    }

    public interface OnSoftKeyBoardChangeListener {
        void keyBoardShow(int height);

        void keyBoardHide(int height);
    }

    public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
        softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
    }
}

3、最后在activity中的主要代码如下:
监听软键盘起来的高度,动态设置phone一栏距离底部的高度,最后将ScrollView自动回滚到最后

  SoftKeyBoardListener.setListener(this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
                Log.e(TAG, "键盘显示 高度" + height);
                tv_phone_line.setPadding(0, 0, 0, height);
                sc_scrollView.fullScroll(ScrollView.FOCUS_DOWN);//滚动到底部
            }

            @Override
            public void keyBoardHide(int height) {
                Log.e(TAG, "键盘隐藏 高度" + height);
                tv_phone_line.setPadding(0, 0, 0, height);
            }
        });

以上方法仅作为个人笔记,并且只适合这种少输入控件的布局页面,要不然会将你要输入的一栏顶到最上面去不可见了

可以使用Android系统提供的软键盘监听器和输入法管理器来实现根据EditText位置显示软键盘的功能。具体步骤如下: 1. 在Activity中注册软键盘监听器: ```java InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.addOnSoftInputChangedListener(onSoftKeyboardChangeListener); ``` 2. 实现软键盘监听器,根据EditText位置动态调整软键盘的显示位置: ```java private final OnSoftKeyboardChangeListener onSoftKeyboardChangeListener = new OnSoftKeyboardChangeListener() { @Override public void onSoftKeyboardShow(int height) { Rect rect = new Rect(); editText.getGlobalVisibleRect(rect); int screenHeight = getResources().getDisplayMetrics().heightPixels; int keyboardHeight = screenHeight - rect.bottom; if (keyboardHeight > 0) { // 软键盘弹出 scrollView.scrollBy(0, keyboardHeight); } } @Override public void onSoftKeyboardHide() { // 软键盘隐藏 scrollView.scrollTo(0, 0); } }; ``` 其中,onSoftKeyboardShow()方法在软键盘弹出时被调用,可以根据EditText的位置动态调整ScrollView的滚动位置,使EditText可见;onSoftKeyboardHide()方法在软键盘隐藏时被调用,可以将ScrollView滚动回原来的位置。 需要注意的是,在AndroidManifest.xml文件中需要为Activity添加以下属性,以确保软键盘监听器能够正常工作: ```xml <activity android:windowSoftInputMode="adjustResize" /> ``` 这样,当软键盘弹出时,系统会自动调整Activity的布局大小,以便软键盘EditText能够同时显示在屏幕上。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值