Android 表情面板和软键盘切换时跳闪问题的解决方法
短信预约 -IT技能 免费直播动态提醒
现在很多应用都会在让用户输入各种文本信息的时候同时多提供一个表情面板,这样就会出现一个问题,即表情面板的跳闪问题要输入文本信息,那固然是需要弹出软键盘,在软键盘显示的情况下,此时如果要切换显示出表情面板,由于表情面板不可能和用户的软键盘高度恰好一样,此外由于控件的上下移位,就会出现表情面板的跳闪现象
在点击切换按钮的时候,表情面板会先向上跳,然后再往下移,这样就会带来很差的用户体验,效果如下图所示:
这里提供一个解决方案,使软键盘和表情面板可以很自然地切换,效果如下图所示:
解决思路主要是这样:系统在弹出软键盘时,会将内容输入框View以及其之上的View都给顶上去,当切换到表情面板时,只有将表情面板的高度保持为和软键盘的高度一致,才能自然地切换。此外,还需要将内容输入框View以及其之上的View的位置固定住,这样才不会出现跳闪问题
主要的操作逻辑都在 EmojiKeyboard 类中
public class EmojiKeyboard { private Activity activity; //文本输入框 private EditText editText; //表情面板 private View emojiPanelView; //内容View,即除了表情布局和输入框布局以外的布局 //用于固定输入框一行的高度以防止跳闪 private View contentView; private InputMethodManager inputMethodManager; private SharedPreferences sharedPreferences; private static final String EMOJI_KEYBOARD = "EmojiKeyboard"; private static final String KEY_SOFT_KEYBOARD_HEIGHT = "SoftKeyboardHeight"; private static final int SOFT_KEYBOARD_HEIGHT_DEFAULT = 654; private Handler handler; public EmojiKeyboard(Activity activity, EditText editText, View emojiPanelView, View emojiPanelSwitchView, View contentView) { init(activity, editText, emojiPanelView, emojiPanelSwitchView, contentView); } private void init(Activity activity, EditText editText, View emojiPanelView, View emojiPanelSwitchView, View contentView) { this.activity = activity; this.editText = editText; this.emojiPanelView = emojiPanelView; this.contentView = contentView; this.editText.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(final View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP && EmojiKeyboard.this.emojiPanelView.isShown()) { lockContentViewHeight(); hideEmojiPanel(true); unlockContentViewHeight(); } return false; } }); this.contentView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_UP) { if (EmojiKeyboard.this.emojiPanelView.isShown()) { hideEmojiPanel(false); } else if (isSoftKeyboardShown()) { hideSoftKeyboard(); } } return false; } }); //用于弹出表情面板的View emojiPanelSwitchView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (EmojiKeyboard.this.emojiPanelView.isShown()) { lockContentViewHeight(); hideEmojiPanel(true); unlockContentViewHeight(); } else { if (isSoftKeyboardShown()) { lockContentViewHeight(); showEmojiPanel(); unlockContentViewHeight(); } else { showEmojiPanel(); } } } }); this.inputMethodManager = (InputMethodManager) this.activity.getSystemService(Context.INPUT_METHOD_SERVICE); this.sharedPreferences = this.activity.getSharedPreferences(EMOJI_KEYBOARD, Context.MODE_PRIVATE); this.activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); this.handler = new Handler(); init(); } private void init() { if (!sharedPreferences.contains(KEY_SOFT_KEYBOARD_HEIGHT)) { handler.postDelayed(new Runnable() { @Override public void run() { showSoftKeyboard(true); } }, 200); } } public boolean interceptBackPress() { if (emojiPanelView.isShown()) { hideEmojiPanel(false); return true; } return false; } private void lockContentViewHeight() { LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) contentView.getLayoutParams(); layoutParams.height = contentView.getHeight(); layoutParams.weight = 0; } private void unlockContentViewHeight() { handler.postDelayed(new Runnable() { @Override public void run() { ((LinearLayout.LayoutParams) contentView.getLayoutParams()).weight = 1; } }, 200); } private int getSoftKeyboardHeight() { Rect rect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); //屏幕当前可见高度,不包括状态栏 int displayHeight = rect.bottom - rect.top; //屏幕可用高度 int availableHeight = ScreenUtils.getAvailableScreenHeight(activity); //用于计算键盘高度 int softInputHeight = availableHeight - displayHeight - ScreenUtils.getStatusBarHeight(activity); Log.e("TAG-di", displayHeight + ""); Log.e("TAG-av", availableHeight + ""); Log.e("TAG-so", softInputHeight + ""); if (softInputHeight != 0) { // 因为考虑到用户可能会主动调整键盘高度,所以只能是每次获取到键盘高度时都将其存储起来 sharedPreferences.edit().putInt(KEY_SOFT_KEYBOARD_HEIGHT, softInputHeight).apply(); } return softInputHeight; } private int getSoftKeyboardHeightLocalValue() { return sharedPreferences.getInt(KEY_SOFT_KEYBOARD_HEIGHT, SOFT_KEYBOARD_HEIGHT_DEFAULT); } private boolean isSoftKeyboardShown() { return getSoftKeyboardHeight() != 0; } private void showSoftKeyboard(boolean saveSoftKeyboardHeight) { editText.requestFocus(); inputMethodManager.showSoftInput(editText, 0); if (saveSoftKeyboardHeight) { handler.postDelayed(new Runnable() { @Override public void run() { getSoftKeyboardHeight(); } }, 200); } } private void hideSoftKeyboard() { inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0); } private void showEmojiPanel() { int softKeyboardHeight = getSoftKeyboardHeight(); if (softKeyboardHeight == 0) { softKeyboardHeight = getSoftKeyboardHeightLocalValue(); } else { hideSoftKeyboard(); } emojiPanelView.getLayoutParams().height = softKeyboardHeight; emojiPanelView.setVisibility(View.VISIBLE); if (emojiPanelVisibilityChangeListener != null) { emojiPanelVisibilityChangeListener.onShowEmojiPanel(); } } private void hideEmojiPanel(boolean showSoftKeyboard) { if (emojiPanelView.isShown()) { emojiPanelView.setVisibility(View.GONE); if (showSoftKeyboard) { showSoftKeyboard(false); } if (emojiPanelVisibilityChangeListener != null) { emojiPanelVisibilityChangeListener.onHideEmojiPanel(); } } } public interface OnEmojiPanelVisibilityChangeListener { void onShowEmojiPanel(); void onHideEmojiPanel(); } private OnEmojiPanelVisibilityChangeListener emojiPanelVisibilityChangeListener; public void setEmoticonPanelVisibilityChangeListener(OnEmojiPanelVisibilityChangeListener emojiPanelVisibilityChangeListener) { this.emojiPanelVisibilityChangeListener = emojiPanelVisibilityChangeListener; }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
Android 表情面板和软键盘切换时跳闪问题的解决方法
下载Word文档到电脑,方便收藏和打印~
下载Word文档
猜你喜欢
Android 表情面板和软键盘切换时跳闪问题的解决方法
现在很多应用都会在让用户输入各种文本信息的时候同时多提供一个表情面板,这样就会出现一个问题,即表情面板的跳闪问题要输入文本信息,那固然是需要弹出软键盘,在软键盘显示的情况下,此时如果要切换显示出表情面板,由于表情面板不可能和用户的软键盘高度
2023-05-30
Android输入法与表情面板切换时的界面抖动问题解决方法
昨天琢磨了下Android的输入法弹出模式,突然发现利用动态切换输入法的弹出模式可以解决输入法抖动的问题。具体是怎样的抖动呢?我们先看微博的反面教材。 【具体表现为:表情面板与输入法面板高度不一致,从而导致弹出输入法(layout被挤压)时
2022-06-06
Android切换至SurfaceView时闪屏(黑屏闪一下)以及黑屏移动问题的解决方法
1.最近的项目中,有一个Activity用到Fragment+ViewPager,其中一个fragment中实现了视频播放的功能,包含有SurfaceView。结果,每次打开程序第一次进入到该Activity时都会闪屏黑一下。原因就出在Su
2022-06-06