最近給公司開發App,體驗的人員覺得登陸界面總是被軟鍵盤擋住了不爽,自行度娘了一大堆發現都是基于Scrollview的或者是把界面整體拔高。但是我下面有注冊按鈕我拔高了又太丑了。
所以感覺不爽,在看了郭霖大神發的推送后根據GoogleBugs里解決方案改寫了里面的解決方案,改為了一套能夠移動部分控件的工具,并且提高了精度。
附上鏈接:SoftKeyBoardAdapter
package com.example.hele.softkeyboardadapter;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.FrameLayout;
/**
* Created by Hele on 2017/5/8.
* 修改自http://mp.weixin.qq.com/s/sWnxIxzNkuTkUaVaPhtqLA
*/
public class SoftHideKeyBoardUtil {
private static boolean sTranslucentStatus = false;
private Activity mContext;
//Root Content View
private View mChildOfContent;
private View mViewContainer;
private int mUsableHeightPrevious;
private int mLastLocation = -1;
private int mFrameSize = -1;
private SoftHideKeyBoardUtil(Activity activity, View container) {
mContext = activity;
mViewContainer = container;
sTranslucentStatus = judgeTranslucentStatus(activity);
//獲取根框
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
//獲取ContentView
mChildOfContent = content.getChildAt(0);
//ViewTreeObserver:監聽界面繪制
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
}
/**
* Call this method to prevent your view such as EditText, LoginButton or other
* from being blocked by a soft keyboard
*
* @param activity : current activity
* @param view_container_to_move : your view group
*/
public static void assistActivity(Activity activity, View view_container_to_move) {
new SoftHideKeyBoardUtil(activity, view_container_to_move);
}
private static boolean judgeTranslucentStatus(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if ((WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS & activity.getWindow().getAttributes().flags)
== WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) {
return true;
}
}
return false;
}
/**
* Adjust the location of your container
*/
private void possiblyResizeChildOfContent() {
if (mLastLocation < 0) {
mLastLocation = (int) mViewContainer.getY();
//Use the beginning as default
mUsableHeightPrevious = computeUsableHeight();
mFrameSize = mUsableHeightPrevious;
}
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != mUsableHeightPrevious) {
int heightKeyboard = mFrameSize - usableHeightNow;
int heightDifference = mUsableHeightPrevious - usableHeightNow;
float adjustY = mLastLocation;
//監聽鍵盤變化
if (heightKeyboard > (mFrameSize / 4) && heightDifference > (mFrameSize / 4)) {
//第二個條件是必須的,判斷鍵盤彈起
//When full screen or translucentStatus is true
int statusBar = sTranslucentStatus ? DisplayUtil.getStatusBarHeight(mContext) : 0;
adjustY = mViewContainer.getY() - mViewContainer.getBottom() + usableHeightNow + statusBar;
} else if (heightKeyboard == 0) {
//收起鍵盤
} else {
//中英文切換
//中文切英文 : dif < 0 . 反之, dif > 0
adjustY = mViewContainer.getY() - heightDifference;
}
mViewContainer.setY(adjustY);
mChildOfContent.requestLayout();
mUsableHeightPrevious = usableHeightNow;
}
}
/**
* Compute Visible Height
* @return
*/
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
}
后來發現有人解釋過了所以我就不重新解釋一遍了。
附上鏈接: 解釋鏈接
除了possiblyResizeChildOfContent方法,其他次要內容和Google上面的差不多。主要是添加了渲染模式的處理、中英文的切換。如果采用原來的方式用在內部控件是無法處理中英文切換的,有興趣的話可以嘗試一下。