Android 软键盘切换表情为什么出现抖动(如何解决抖动)
程序员文章站
2022-03-09 07:57:24
软键盘切换表情页面进行闪动1 为什么会软键盘切换表情会出现页面闪动?2 弹出软键盘的逻辑3 显示表情的方法4 表情控件显示高度和软键盘高度一致1 为什么会软键盘切换表情会出现页面闪动?怎么解决呢?
Android 软键盘造成页面进行抖动
1 为什么会软键盘切换表情会出现页面闪动?
怎么解决呢?
<activity
android:name=".activity.ChatActivity"
android:screenOrientation="portrait"
android:theme="@style/FormTheme"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden" />
关键点就在于这个windowSoftInputMode,
adjustResize:软键盘弹出时,顶起布局,软键盘隐藏时,布局还原
还有另一个mode
adjustNothing:软键盘弹出隐藏,布局始终无变化
当然,也可以代码设置这些Mode
public static void updateSoftInputMethod(Activity activity, int softInputMode) {
if (!activity.isFinishing()) {
WindowManager.LayoutParams params = activity.getWindow().getAttributes();
if (params.softInputMode != softInputMode) {
params.softInputMode = softInputMode;
activity.getWindow().setAttributes(params);
}
}
}
2 弹出软键盘的逻辑
1.判断表情是否显示,显示就才有延迟进行关闭表情
findViewById(R.id.et_input).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
InputMethodUtils.setKeyboardShowing(true);
if (isEmotionPanelShowing()) {
mEmotionPanel.postDelayed(mHideEmotionPanelTask, 200);
}
}
return false;
}
});
2,关闭表情的方法
/**
* 采用延迟操作,关闭表情
*/
private Runnable mHideEmotionPanelTask = new Runnable() {
@Override
public void run() {
hideEmotionPanel();
}
};
/**
* 隐藏表情
*/
public void hideEmotionPanel() {
if (mEmotionPanel.getVisibility() != View.GONE) {
mEmotionPanel.setVisibility(View.GONE);
InputMethodUtils.updateSoftInputMethod(this, WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
}
3 显示表情的方法
/**
* 切换表情
*/
findViewById(R.id.iv_switcher).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isEmotionPanelShowing()) {
mEmotionPanel.postDelayed(mHideEmotionPanelTask, 200);
InputMethodUtils.toggleSoftInput(getCurrentFocus());
} else {
showEmotionPanel();
}
}
});
/**
* 判断表情是否显示
* @return
*/
public boolean isEmotionPanelShowing() {
return mEmotionPanel.getVisibility() == View.VISIBLE;
}
/**
* 显示表情的逻辑
* 1,改变 WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING
* 2,显示表情
* 3,关闭软键盘
*/
public void showEmotionPanel() {
mEmotionPanel.removeCallbacks(mHideEmotionPanelTask);
InputMethodUtils.updateSoftInputMethod(this, WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
mEmotionPanel.setVisibility(View.VISIBLE);
InputMethodUtils.hideKeyboard(getCurrentFocus());
}
这样切换表情不会闪动了
4 表情控件显示高度和软键盘高度一致
1.获取软键盘高度
/**
* 只有当Activity的windowSoftInputMode设置为adjustResize时才有效
*/
public static void detectKeyboard(final Activity activity, final OnKeyboardEventListener listener) {
final View activityRootView = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
if (activityRootView != null) {
ViewTreeObserver viewTreeObserver = activityRootView.getViewTreeObserver();
if (viewTreeObserver != null) {
//OnGlobalLayoutListener检测软键盘是否弹出
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
final Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = DisplayUtils.getScreenHeight() - (r.bottom - r.top);
boolean show = heightDiff >= sKeyBoardHeight / 2;
if (show && sIsKeyboardShowing) {
if (show) {
int keyboardHeight = heightDiff - DisplayUtils.getStatusBarHeight();
if (keyboardHeight != sKeyBoardHeight) {
sKeyBoardHeight = keyboardHeight;
if (listener != null) {
listener.updateEmotionPanelHeight(keyboardHeight);
}
}
if (listener != null) {
listener.onSoftKeyboardOpened();
}
} else {
if (listener != null) {
listener.onSoftKeyboardClosed();
}
}
}
}
});
}
}
}
2,把软键盘高度赋值给表情控件高度
public void updateEmotionHeight(int keyboardHeight) {
ViewGroup.LayoutParams params = mEmotionPanel.getLayoutParams();
if (params != null && params.height != keyboardHeight) {
params.height = keyboardHeight;
mEmotionPanel.setLayoutParams(params);
}
}
最后附上demo的下载链接;
https://download.csdn.net/download/lilinhualisen/13188461
demo
本文地址:https://blog.csdn.net/lilinhualisen/article/details/110122195