软键盘相关问题总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yoonerloop/article/details/85563927

 

之前写了一篇文章,主要介绍一些EditText的一些属性和自定义打开链接(点击打开),最近开发中又遇到了其他的的问题,再次进行总结。包含以下几个部分:

  1. 软键盘顶部添加布局方式
  2. 防止软键盘顶起布局内容
  3. 初始进入页面软键盘弹出
  4. 软键盘的弹出与收缩
  5. 软键盘弹出与收缩的监听
     

一、软键盘顶部添加布局方式

方法一:Activity配置信息

1、在清单文件中配置的该Activity的节点中配置

    android:windowSoftInputMode="stateVisible|adjustResize|stateHidden"
    android:screenOrientation="portrait"

2、XML文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        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"
        tools:context="com.demo.myapplication.MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="啦啦啦"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="啦啦啦"/>
        </LinearLayout>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:background="@null"
            android:gravity="center"
            android:hint="请输入内容"
            android:textSize="14sp"/>

    </RelativeLayout>

方法二:采用ScrollView

采用Scrollview方法与Activity或者fragment没有关系,不需要节点中配置其他信息,如下:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        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"
        tools:context="com.demo.myapplication.MainActivity">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="啦啦啦"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="啦啦啦"/>
            </LinearLayout>
        </ScrollView>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:background="@null"
            android:gravity="center"
            android:hint="请输入内容"
            android:textSize="14sp"/>

    </RelativeLayout>

这两种方法使用起来都很方便,采用哪个都可以,二者唯一的区别就是采用ScrollView内容布局可以在键盘弹起来的状态进行滑动,类似于微信软键盘打开的状态,而采用Activity配置信息内容栏就是固定的,不可滑动。

二、防止软键盘顶起布局内容

如果想要布局不被顶起来,需要在Activity中配置:

android:windowSoftInputMode="adjustResize|stateHidden"

这样软键盘就会覆盖在内容上。

三、初始进入页面软键盘弹出

有时候刚刚进入fragment或者Activity就会立即弹出来软键盘,这样体验不好,为了避免这种情况,需要在edittext的父布局上添加:

    android:focusable="true"
    android:focusableInTouchMode="true"

或者在Activity的清单文件节点上配置:

    android:windowSoftInputMode="stateHidden"

这样在手动点击edittext的时候才会弹出软键盘。

四、软键盘的弹出与收缩

   public static void showKeyboard(View view) {
       InputMethodManager imm = (InputMethodManager) view.getContext()
               .getSystemService(Context.INPUT_METHOD_SERVICE);
       if (imm != null) {
           view.requestFocus();
           imm.showSoftInput(view, 0);
       }
   }

   public static void hideKeyboard(View view){
       InputMethodManager imm = (InputMethodManager) view.getContext()
               .getSystemService(Context.INPUT_METHOD_SERVICE);
       if (imm != null) {
           imm.hideSoftInputFromWindow(view.getWindowToken(),0);
       }
   }

五、软键盘弹出与收缩的监听

public class KeyboardLayout extends RelativeLayout {

    public static final byte KEYBOARD_STATE_SHOW = -3;
    public static final byte KEYBOARD_STATE_HIDE = -2;
    public static final byte KEYBOARD_STATE_INIT = -1;
    private boolean mHasInit;
    private boolean mHasKeybord;
    private int mHeight;
    private onKeyboaddsChangeListener mListener;

    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 void setOnkbdStateListener(onKeyboaddsChangeListener listener) {
        mListener = listener;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!mHasInit) {
            mHasInit = true;
            mHeight = b;
            if (mListener != null) {
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
            }
        } else {
            mHeight = mHeight < b ? b : mHeight;
        }
        if (mHasInit && mHeight > b) {
            mHasKeybord = true;
            if (mListener != null) {
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
            }
        }
        if (mHasInit && mHasKeybord && mHeight == b) {
            mHasKeybord = false;
            if (mListener != null) {
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
            }
        }
    }

    public interface onKeyboaddsChangeListener {
         void onKeyBoardStateChange(int state);
    }
}

在使用的时候需要实现onKeyboaddsChangeListener接口,重写对应的方法,需要注意的是该控件需要作为软键盘监听页面的跟布局,否则监听不要状态。

猜你喜欢

转载自blog.csdn.net/yoonerloop/article/details/85563927