1.設定布局随着軟鍵盤的彈出而上移
描述:在點選文本框輸入文字時,如何設定界面底部控件随着軟鍵盤的彈出而上移。(非整體界面上移哦)具體效果如下面圖所示:

鍵盤彈起效果圖.jpeg

鍵盤隐藏效果圖.png
這種效果是如何做到的呢
其實要完成這樣的效果很簡單,首先要監聽輸入法的彈起與消失,其次根據輸入法狀态調用。view.requestLayout
輸入法監聽
public class SoftKeyBroadManager implements ViewTreeObserver.OnGlobalLayoutListener {
public interface SoftKeyboardStateListener {
void onSoftKeyboardOpened(int keyboardHeightInPx);
void onSoftKeyboardClosed();
}
private final List listeners = new LinkedList();
private final View activityRootView;
private int lastSoftKeyboardHeightInPx;
private boolean isSoftKeyboardOpened;
public SoftKeyBroadManager(View activityRootView) {
this(activityRootView, false);
}
public SoftKeyBroadManager(View activityRootView, boolean isSoftKeyboardOpened) {
this.activityRootView = activityRootView;
this.isSoftKeyboardOpened = isSoftKeyboardOpened;
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
}
@Override
public void onGlobalLayout() {
final Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (!isSoftKeyboardOpened && heightDiff > 500) {
// 如果高度超過500 鍵盤可能被打開
isSoftKeyboardOpened = true;
notifyOnSoftKeyboardOpened(heightDiff);
} else if (isSoftKeyboardOpened && heightDiff < 500) {
isSoftKeyboardOpened = false;
notifyOnSoftKeyboardClosed();
}
}
public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
this.isSoftKeyboardOpened = isSoftKeyboardOpened;
}
public boolean isSoftKeyboardOpened() {
return isSoftKeyboardOpened;
}
public int getLastSoftKeyboardHeightInPx() {
return lastSoftKeyboardHeightInPx;
}
public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
listeners.add(listener);
}
public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
listeners.remove(listener);
}
private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;
for (SoftKeyboardStateListener listener : listeners) {
if (listener != null) {
listener.onSoftKeyboardOpened(keyboardHeightInPx);
}
}
}
private void notifyOnSoftKeyboardClosed() {
for (SoftKeyboardStateListener listener : listeners) {
if (listener != null) {
listener.onSoftKeyboardClosed();
}
}
}
}
Activity中使用,并調用view的requestLayout()
public class MainActivity extends AppCompatActivity {
private LinearLayout mBottomView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View root = findViewById(R.id.root);
mBottomView = findViewById(R.id.bottom);
SoftKeyBroadManager softKeyBroadManager = new SoftKeyBroadManager(root);
softKeyBroadManager.addSoftKeyboardStateListener(softKeyboardStateListener);
}
SoftKeyBroadManager.SoftKeyboardStateListener softKeyboardStateListener = new SoftKeyBroadManager.SoftKeyboardStateListener() {
@Override
public void onSoftKeyboardOpened(int keyboardHeightInPx) {
mBottomView.requestLayout();
}
@Override
public void onSoftKeyboardClosed() {
mBottomView.requestLayout();
}
};
}
注意事項
(1)在清單檔案中給activity設定屬性android:windowSoftInputMode="adjustResize"一些不必要的屬性不要随意添加,以免影響實作功能效果。
(2)底部控件View設定為android:layout_alignParentBottom="true"
(3)需要根據輸入法狀态設定view.requestLayout();
(4)有些設定沉浸式可能導緻失效,解決辦法是 在根布局設定沉浸式設定問題導緻,嘗試根布局設定fitSystemWindows=true