欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 完美判断软键盘高度的方法

程序员文章站 2022-04-20 14:49:28
...

首先自定义一个ViewGroup,可以继承已有的LinearLayout等

public class KeyboardLayout extends LinearLayout {
public KeyboardLayout(Context context) {
    super(context);
}

public KeyboardLayout(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public KeyboardLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public KeyboardLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

}

private int mLayoutHeight = 0;
private int mKeyboardHeight=0;


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int h = resolveSize(0, heightMeasureSpec);
    mLayoutHeight = Math.max(mLayoutHeight, h);  //这里记录原始布局
    mKeyboardHeight=Math.max(mKeyboardHeight,Math.abs(mLayoutHeight -h));
    if(mKeyboardHeight>mLayoutHeight/5){
        存储这个软键盘高度
    }
    
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}

在login页面定义大致如下

核心是KeyboardLayout里面必须有一个ScrollView布局

  <LinearLayout>
      <KeyboardLayout>
              <ScrollView>
                      <EditText/>
                 </ScrollView>
      </KeyboardLayout>
 </LinearLayout>

下面是我的实际代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/default_content_background"
    android:orientation="vertical"
    tools:context=".modules.login.views.LoginActivity">

    <com.maple.subway.modules.login.views.layouts.KeyboardLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:background="@color/default_font_color" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="30dp">

                <com.maple.subway.commons.android.views.widgets.XTextView
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_marginTop="40dp"
                    android:text="@string/login_title"
                    android:textColor="@color/default_font_color"
                    android:textSize="28sp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="25dp"
                    android:orientation="horizontal"
                    android:padding="5dp">

                    <com.maple.subway.commons.android.views.widgets.XTextView
                        android:layout_width="100dp"
                        android:layout_height="40dp"
                        android:text="@string/login_label_mobile"
                        android:textColor="@color/default_font_color"
                        android:textSize="20sp" />

                    <com.maple.subway.modules.login.views.ClearEditText
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_marginRight="30dp"

                        android:background="@null"
                        android:hint="@string/login_hint_mobile"
                        android:maxLines="1"
                        android:textColor="@color/login_edittext_color"
                        android:textColorHint="@color/login_edittext_hint_color"
                        android:textSize="20sp" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="5dp"
                    android:background="@color/default_color"></LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:orientation="horizontal"
                    android:padding="5dp"
                    android:visibility="gone">

                    <com.maple.subway.commons.android.views.widgets.XTextView
                        android:layout_width="100dp"
                        android:layout_height="40dp"
                        android:text="@string/login_label_password"
                        android:textColor="@color/default_font_color"
                        android:textSize="20sp" />

                    <com.maple.subway.commons.android.views.widgets.XEditText
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_marginRight="30dp"

                        android:background="@null"
                        android:hint="@string/login_hint_password"
                        android:inputType="text"
                        android:maxLines="1"
                        android:textColor="@color/login_edittext_color"
                        android:textColorHint="@color/login_edittext_hint_color"
                        android:textSize="20sp" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:orientation="horizontal"
                    android:padding="5dp">

                    <com.maple.subway.commons.android.views.widgets.XTextView
                        android:layout_width="100dp"
                        android:layout_height="40dp"
                        android:text="@string/login_label_message"
                        android:textColor="@color/default_font_color"
                        android:textSize="20sp" />

                    <com.maple.subway.modules.login.views.ClearEditText
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:layout_weight="1"

                        android:background="@null"
                        android:hint="@string/login_hint_message"
                        android:inputType="text"
                        android:maxLines="1"
                        android:textColor="@color/login_edittext_color"
                        android:textColorHint="@color/login_edittext_hint_color"
                        android:textSize="20sp" />

                    <com.maple.subway.commons.android.views.widgets.XButton
                        style="?android:attr/borderlessButtonStyle"
                        android:layout_width="110dp"
                        android:layout_height="32dp"
                        android:layout_marginBottom="2dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/login_button_background_message"
                        android:text="@string/login_send_message"
                        android:textColor="@color/default_font_color"
                        android:textSize="16sp" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="5dp"
                    android:background="@color/default_color"></LinearLayout>

                <com.maple.subway.commons.android.views.widgets.XTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="25dp"
                    android:padding="3dp"
                    android:text="@string/login_use_message"
                    android:textColor="@color/login_text_link_color"
                    android:textSize="20sp" />

                <com.maple.subway.commons.android.views.widgets.XButton
                    android:id="@+id/login_button_submit"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginTop="35dp"
                    android:background="@drawable/login_button_background_submit"
                    android:text="@string/login_button_submit"
                    android:textAlignment="center"
                    android:textColor="@android:color/white"
                    android:textSize="20sp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"></LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <com.maple.subway.commons.android.views.widgets.XTextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:text="@string/login_find_password"
                        android:textColor="@color/login_text_link_color"
                        android:textSize="16sp" />

                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="15dp"
                        android:layout_marginRight="15dp"
                        android:background="@color/login_edittext_hint_color"></LinearLayout>

                    <com.maple.subway.commons.android.views.widgets.XEditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:text="@string/login_lock_account"
                        android:textColor="@color/login_text_link_color"
                        android:textSize="16sp" />
                </LinearLayout>
            </LinearLayout>
        </ScrollView>

        <com.maple.subway.commons.android.views.widgets.XEditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="@string/login_lock_account"
            android:textColor="@color/login_text_link_color"
            android:textSize="16sp" />
    </com.maple.subway.modules.login.views.layouts.KeyboardLayout>

</LinearLayout>

这样当软键盘出现以后,就会触发KeyboardLayout的onMeasure方法,两次高度计算出来的就是软键盘的高度。这个可以忽略机型和品牌的影响。

同时,也就是APP在启动时,必须引导用户触摸出现一次软键盘,不然就只能用我另外一个方法Android 获取底部导航栏高度