欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 软键盘切换表情为什么出现抖动(如何解决抖动)

程序员文章站 2022-06-16 13:18:09
软键盘切换表情页面进行闪动1 为什么会软键盘切换表情会出现页面闪动?2 弹出软键盘的逻辑3 显示表情的方法4 表情控件显示高度和软键盘高度一致1 为什么会软键盘切换表情会出现页面闪动?怎么解决呢?

1 为什么会软键盘切换表情会出现页面闪动?

Android 软键盘切换表情为什么出现抖动(如何解决抖动)
Android 软键盘切换表情为什么出现抖动(如何解决抖动)
Android 软键盘切换表情为什么出现抖动(如何解决抖动)

怎么解决呢?

<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

相关标签: android