前言
在上节,我们讲了一个监听软键盘弹出和隐藏的布局—KeyboardLayout。感兴趣的可以参考我的另一篇文章:
[监听软键盘弹起和隐藏的布局—KeyboardLayout]
但是个人感觉使用还是有些繁琐。对布局的使用限制比较多:要么根布局使用KeyboardLayout,当根布局不是KeyboardLayout的时候,你还得将你的布局外面套一层KeyboardLayout布局,这样又导致布局层级较多。所以,今天我又将此代码做了一个提取,封装成了一个工具类—SoftKeyBoardHelper。使用简单,对布局类型无限制。下面就来讲讲它的使用吧。
今天涉及内容:
- SoftKeyBoardHelper的使用
- SoftKeyBoardHelper在Activity中使用代码
- 效果图和项目结构图
- SoftKeyBoardHelper源码
先来波效果图
一.SoftKeyBoardHelper的使用
SoftKeyBoardHelper监听软键盘的 显示/隐藏 方法如下:
/**监听软键盘的 显示/隐藏 **/
public static void setOnListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener);
在manifast.xml中对应的activity(此处以TestActivity为例)注册时,添加如下配置:
<activity android:name=".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代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<TextView
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"/>
<EditText
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"/>
</LinearLayout>
三.效果图和项目结构图
效果图
项目结构图
四.SoftKeyBoardHelper源码
SoftKeyBoardHelper源码如下: