android softkeyboard,封装一个软键盘监听工具类—SoftKeyBoardHelper

前言

在上节,我们讲了一个监听软键盘弹出和隐藏的布局—KeyboardLayout。感兴趣的可以参考我的另一篇文章:

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

但是个人感觉使用还是有些繁琐。对布局的使用限制比较多:要么根布局使用KeyboardLayout,当根布局不是KeyboardLayout的时候,你还得将你的布局外面套一层KeyboardLayout布局,这样又导致布局层级较多。所以,今天我又将此代码做了一个提取,封装成了一个工具类—SoftKeyBoardHelper。使用简单,对布局类型无限制。下面就来讲讲它的使用吧。

今天涉及内容:

SoftKeyBoardHelper的使用

SoftKeyBoardHelper在Activity中使用代码

效果图和项目结构图

SoftKeyBoardHelper源码

先来波效果图

68c906814fa9

1.gif

一.SoftKeyBoardHelper的使用

SoftKeyBoardHelper监听软键盘的 显示/隐藏 方法如下:

/**监听软键盘的 显示/隐藏 **/

public static void setOnListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener);

在manifast.xml中对应的activity(此处以TestActivity为例)注册时,添加如下配置:

android:configChanges="keyboardHidden|orientation|screenSize|touchscreen"

android:screenOrientation="portrait"/>

在activity中监听软键盘的显示和隐藏,你可以类似下面这样:

//监听软键盘的显示和隐藏

SoftKeyBoardHelper.setOnListener(TestActivity.this, new SoftKeyBoardHelper.OnSoftKeyBoardChangeListener() {

@Override

public void keyBoardShow(int height) {

//软键盘显示时处理你的逻辑 (height为软键盘高度)

//......

}

@Override

public void keyBoardHide(int height) {

//软键盘隐藏时处理你的逻辑 (height为软键盘高度)

//......

}

});

二.SoftKeyBoardHelper在Activity中使用代码

下面贴出 SoftKeyBoardHelper在Activity中使用代码:

/**

* Title:

* description:

* autor:pei

* created on 2019/11/8

*/

public class TestActivity extends AppCompatActivity {

private TextView mTv;

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_test);

mTv=findViewById(R.id.tv);

initData();

setListener();

}

private void initData(){

showTextMessage(false);

}

private void showTextMessage(boolean isShow){

mTv.setText(isShow?"软键盘弹出":"软键盘隐藏");

}

private void setListener() {

//监听软键盘的显示和隐藏

SoftKeyBoardHelper.setOnListener(TestActivity.this, new SoftKeyBoardHelper.OnSoftKeyBoardChangeListener() {

@Override

public void keyBoardShow(int height) {

LogUtil.i("======软键盘弹出=======键盘高度=" + height);

ToastUtil.shortShow("======软键盘弹出=======键盘高度=" + height);

showTextMessage(true);

}

@Override

public void keyBoardHide(int height) {

LogUtil.i("======软键盘收起=======键盘高度=" + height);

ToastUtil.shortShow("======软键盘收起=======键盘高度=" + height);

showTextMessage(false);

}

});

}

}

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

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"

android:orientation="vertical">

android:id="@+id/tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="120dp"

android:gravity="center"

android:text="我是TestActivity"

android:textColor="@color/black"

android:textSize="14sp"/>

android:layout_width="120dp"

android:layout_height="40dp"

android:layout_gravity="center_horizontal"

android:layout_marginTop="20dp"

android:textColorHint="#aaaaaa"

android:textColor="#000000"

android:textSize="14sp"/>

三.效果图和项目结构图

效果图

68c906814fa9

1.gif

项目结构图

68c906814fa9

image.png

四.SoftKeyBoardHelper源码

SoftKeyBoardHelper源码如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值