现在很多应用都会在让用户输入各种文本信息的时候同时多提供一个表情面板,这样就会出现一个问题,即表情面板的跳闪问题要输入文本信息,那固然是需要弹出软键盘,在软键盘显示的情况下,此时如果要切换显示出表情面板,由于表情面板不可能和用户的软键盘高度恰好一样,此外由于控件的上下移位,就会出现表情面板的跳闪现象
在点击切换按钮的时候,表情面板会先向上跳,然后再往下移,这样就会带来很差的用户体验,效果如下图所示:
这里提供一个解决方案,使软键盘和表情面板可以很自然地切换,效果如下图所示:
解决思路主要是这样:系统在弹出软键盘时,会将内容输入框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
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容- 如何在 Java 中实现对正方形的缩放操作?(如何在Java中对正方形进行缩放操作)
- 如何正确使用 Java 的 join 方法?(java join方法怎么使用)
- Java 中 DecimalFormat 在哪些场景下使用较为合适?(Java DecimalFormat在哪里使用合适)
- 如何确保Redis客户端的安全性:实用技巧与最佳实践
- 在 JavaScript 中如何使用 parentNode?(javascript中的parentNode怎么用)
- 如何高效编码 Java Supplier 接口?(java supplier接口的高效编码技巧)
- 如何进行 Java NoSQL 查询优化?(java nosql查询优化怎样进行)
- Java 中 `equals()` 的核心究竟是什么?(java eques的核心是什么)
- Java代理模式的优缺点分别有哪些?(Java代理模式有哪些优缺点)
- 2024下半年北京软考成绩复查时间及流程
猜你喜欢
AI推送时光机Android 表情面板和软键盘切换时跳闪问题的解决方法
后端开发2023-05-30
Android输入法与表情面板切换时的界面抖动问题解决方法
后端开发2022-06-06
咦!没有更多了?去看看其它编程学习网 内容吧