前言
在Android开发过程中,有时涉及到软键盘显示和隐藏动作的监听问题。KeyboardLayout,一个自定义布局,继承自FrameLayout,很好的实现了软键盘监听的问题,那么今天就来讲讲KeyboardLayout布局的使用吧。
今天涉及的内容:
- KeyboardLayout使用前的基本设置
- KeyboardLayout在MainActivity中使用
- 效果图和项目结构图
- KeyboardLayout源码
先来波效果图
一.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>
三.效果图和项目结构图
效果图:
项目结构图
四.KeyboardLayout源码
KeyboardLayout源码如下: