前言
在Android開發(fā)過程中憔古,有時涉及到軟鍵盤顯示和隱藏動作的監(jiān)聽問題遮怜。KeyboardLayout,一個自定義布局投放,繼承自FrameLayout,很好的實現(xiàn)了軟鍵盤監(jiān)聽的問題适贸,那么今天就來講講KeyboardLayout布局的使用吧灸芳。
今天涉及的內(nèi)容:
- KeyboardLayout使用前的基本設(shè)置
- KeyboardLayout在MainActivity中使用
- 效果圖和項目結(jié)構(gòu)圖
- KeyboardLayout源碼
先來波效果圖
一.KeyboardLayout使用前的基本設(shè)置
若你要在TestActivity中監(jiān)聽軟鍵盤的顯示和隱藏,則需要將TestActivity對應(yīng)的布局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);
然后要監(jiān)聽軟鍵盤烙样,則像下面這樣:
//監(jiān)聽軟鍵盤彈出與隱藏
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(){
//監(jiān)聽軟鍵盤彈出與隱藏
mKeyboardLayout.setKeyboardListener(new KeyboardLayout.KeyboardLayoutListener() {
@Override
public void onKeyboardStateChanged(boolean isActive, int keyboardHeight) {
if(isActive){
//軟鍵盤彈出
showTextMessage(true);
}else{
//軟鍵盤隱藏
showTextMessage(false);
}
}
});
}
}
TestActivity 對應(yīng)布局 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>
三.效果圖和項目結(jié)構(gòu)圖
效果圖:
項目結(jié)構(gòu)圖
四.KeyboardLayout源碼
KeyboardLayout源碼如下: