檢測軟鍵盤的彈起與隱藏【絕對經典,好用】
阿新 • • 發佈:2019-01-06
今天看到社群裡面有人問關於如何檢測軟鍵盤的彈起和隱藏事件。正確處理好軟鍵盤的彈起和隱藏可以大大提升應用的體驗。這一點,“切客優惠”做的很好。
在軟鍵盤彈起後,下面的分享內容自動隱藏,並且在titlebar上,顯示簽到按鈕。這個是一個非常貼心的設計,使用者體檢大大提升。
這是被大家視為無解的難題,我給大家提供一種方法。
有圖為證:
初始化頁面
刀客優化版初始頁
優化版鍵盤彈起
軟鍵盤隱藏
嘿嘿,未優化版的釘子戶
釘子戶果然堅挺啊
下面談談偶的實現原理:
使用自定義佈局,頁面佈局中包含ScrollVIew,在軟鍵盤彈起後,佈局的高度會發生改變,根據佈局的高度來判斷軟鍵盤的狀態。
-
package com.ransj.keyboard;
-
import android.content.Context;
-
import android.util.AttributeSet;
-
import android.util.Log;
-
import android.widget.RelativeLayout;
-
public class KeyboardLayout extends RelativeLayout {
-
private static final String TAG = KeyboardLayout.class.getSimpleName();
-
public static final byte KEYBOARD_STATE_SHOW = -3;
-
public static final byte KEYBOARD_STATE_HIDE = -2;
-
public static final byte KEYBOARD_STATE_INIT = -1;
-
private boolean mHasInit;
-
private boolean mHasKeybord;
-
private int mHeight;
-
private onKybdsChangeListener mListener;
-
public KeyboardLayout(Context context, AttributeSet attrs, int defStyle) {
-
super(context, attrs, defStyle);
-
}
-
public KeyboardLayout(Context context, AttributeSet attrs) {
-
super(context, attrs);
-
}
-
public KeyboardLayout(Context context) {
-
super(context);
-
}
-
/**
-
* set keyboard state listener
-
*/
-
public void setOnkbdStateListener(onKybdsChangeListener listener){
-
mListener = listener;
-
}
-
@Override
-
protected void onLayout(boolean changed, int l, int t, int r, int b) {
-
super.onLayout(changed, l, t, r, b);
-
if (!mHasInit) {
-
mHasInit = true;
-
mHeight = b;
-
if (mListener != null) {
-
mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
-
}
-
} else {
-
mHeight = mHeight < b ? b : mHeight;
-
}
-
if (mHasInit && mHeight > b) {
-
mHasKeybord = true;
-
if (mListener != null) {
-
mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
-
}
-
Log.w(TAG, "show keyboard.......");
-
}
-
if (mHasInit && mHasKeybord && mHeight == b) {
-
mHasKeybord = false;
-
if (mListener != null) {
-
mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
-
}
-
Log.w(TAG, "hide keyboard.......");
-
}
-
}
-
public interface onKybdsChangeListener{
-
public void onKeyBoardStateChange(int state);
-
}
- }
-
<?xml version="1.0" encoding="utf-8"?>
-
<com.ransj.keyboard.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:id="@+id/keyboardLayout1"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent" >
-
<ScrollView
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:layout_alignParentLeft="true"
-
android:layout_alignParentTop="true"
-
android:fillViewport="true" >
-
<LinearLayout
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:orientation="vertical" >
-
<TextView
-
android:id="@+id/testone_tv"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:background="#000000"
-
android:gravity="center"
-
android:text="軟體盤彈起,我將消失!軟鍵盤隱藏,我將回來!"
-
android:layout_weight="1.0"
-
android:textColor="#00ff00"
-
android:textStyle="bold" />
-
<EditText
-
android:id="@+id/testone_et"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"></EditText>
-
</LinearLayout>
-
</ScrollView>
- </com.ransj.keyboard.KeyboardLayout>
-
package com.ransj.keyboard;
-
import com.ransj.keyboard.KeyboardLayout.onKybdsChangeListener;
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.TextView;
-
import android.widget.Toast;
-
/**
-
*
-
*
-
*/
-
public class TestOne extends Activity{
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.testone);
-
KeyboardLayout mainView = (KeyboardLayout) findViewById(R.id.keyboardLayout1);
-
final TextView tv = (TextView) findViewById(R.id.testone_tv);
-
mainView.setOnkbdStateListener(new onKybdsChangeListener() {
-
public void onKeyBoardStateChange(int state) {
-
switch (state) {
-
case KeyboardLayout.KEYBOARD_STATE_HIDE:
-
tv.setVisibility(View.VISIBLE);
-
Toast.makeText(getApplicationContext(), "軟鍵盤隱藏", Toast.LENGTH_SHORT).show();
-
break;
-
case KeyboardLayout.KEYBOARD_STATE_SHOW:
-
tv.setVisibility(View.INVISIBLE);
-
Toast.makeText(getApplicationContext(), "軟鍵盤彈起", Toast.LENGTH_SHORT).show();
-
break;
-
}
-
}
-
});
-
}
-
@Override
-
protected void onDestroy() {
-
super.onDestroy();
-
}
-
@Override
-
protected void onPause() {
-
super.onPause();
-
}
- }