监听软键盘弹起和隐藏的布局—KeyboardLayout

前言

在Android开发过程中,有时涉及到软键盘显示和隐藏动作的监听问题。KeyboardLayout,一个自定义布局,继承自FrameLayout,很好的实现了软键盘监听的问题,那么今天就来讲讲KeyboardLayout布局的使用吧。

今天涉及的内容:

  1. KeyboardLayout使用前的基本设置
  2. KeyboardLayout在MainActivity中使用
  3. 效果图和项目结构图
  4. KeyboardLayout源码

先来波效果图


1.gif

一.KeyboardLayout使用前的基本设置

若你要在TestActivity中监听软键盘的显示和隐藏,则需要将TestActivity对应的布局activity_test.xml将根布局替换为KeyboardLayout,类似如下:

<?xml version="1.0" encoding="utf-8"?>
<com.example.function.KeyboardLayout 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:id="@+id/container_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".MainActivity">

    //其他代码省略
    //......    

</com.example.function.KeyboardLayout>

然后在 manifast.xml中给TestActivity添加以下配置:

<activity android:name=".TestActivity"
     android:configChanges="keyboardHidden|orientation|screenSize|touchscreen"
     android:screenOrientation="portrait"/>

二. KeyboardLayout在MainActivity中使用

声明及初始化

    //声明
    private KeyboardLayout mKeyboardLayout;

   //初始化
   mKeyboardLayout=findViewById(R.id.container_layout);

然后要监听软键盘,则像下面这样:

        //监听软键盘弹出与隐藏
        mKeyboardLayout.setKeyboardListener(new KeyboardLayout.KeyboardLayoutListener() {
            @Override
            public void onKeyboardStateChanged(boolean isActive, int keyboardHeight) {
                if(isActive){
                    //软键盘弹出
                    showTextMessage(true);
                }else{
                    //软键盘隐藏
                    showTextMessage(false);
                }
            }
        });

下面贴出KeyboardLayout在TestActivity 中使用代码:

public class TestActivity extends AppCompatActivity {

    //声明
    private KeyboardLayout mKeyboardLayout;

    private TextView mTv;
    private TextView mTv1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        mKeyboardLayout=findViewById(R.id.container_layout);

        mTv=findViewById(R.id.tv);
        mTv1=findViewById(R.id.tv_find);

        initData();
        setListener();


    }

    private void initData(){
        showTextMessage(false);
    }

    private void showTextMessage(boolean isShow){
        mTv.setText(isShow?"软键盘弹出":"软键盘隐藏");
    }

    private void setListener(){
        //监听软键盘弹出与隐藏
        mKeyboardLayout.setKeyboardListener(new KeyboardLayout.KeyboardLayoutListener() {
            @Override
            public void onKeyboardStateChanged(boolean isActive, int keyboardHeight) {
                if(isActive){
                    //软键盘弹出
                    showTextMessage(true);
                }else{
                    //软键盘隐藏
                    showTextMessage(false);
                }
            }
        });
    }
}

TestActivity 对应布局 activity_test.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<com.example.function.KeyboardLayout 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:id="@+id/container_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="大家好"
        android:textColor="@color/black"
        android:textSize="14sp"/>

    <TextView
        android:id="@+id/tv_find"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="60dp"
        android:gravity="center"
        android:text="测试test"
        android:textColor="@color/black"
        android:textSize="14sp"/>

    <EditText
        android:id="@+id/edt_text"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:background="#00ff00"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="100dp"/>

</com.example.function.KeyboardLayout>

三.效果图和项目结构图

效果图:


1.gif

项目结构图


image.png

四.KeyboardLayout源码

KeyboardLayout源码如下:

还有 32% 的精彩内容
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
支付 ¥2.00 继续阅读

推荐阅读更多精彩内容