1.设置布局随着软键盘的弹出而上移
描述:在点击文本框输入文字时,如何设置界面底部控件随着软键盘的弹出而上移。(非整体界面上移哦)具体效果如下面图所示:这种效果是如何做到的呢
其实要完成这样的效果很简单,首先要监听输入法的弹起与消失,其次根据输入法状态调用。view.requestLayout
输入法监听
public class SoftKeyBroadManager implements ViewTreeObserver.OnGlobalLayoutListener {
public interface SoftKeyboardStateListener {
void onSoftKeyboardOpened(int keyboardHeightInPx);
void onSoftKeyboardClosed();
}
private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();
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