Android 表情面板和软键盘切换时跳闪问题的解决方法
程序员文章站
2023-12-09 14:59:45
现在很多应用都会在让用户输入各种文本信息的时候同时多提供一个表情面板,这样就会出现一个问题,即表情面板的跳闪问题要输入文本信息,那固然是需要弹出软键盘,在软键盘显示的情况下...
现在很多应用都会在让用户输入各种文本信息的时候同时多提供一个表情面板,这样就会出现一个问题,即表情面板的跳闪问题要输入文本信息,那固然是需要弹出软键盘,在软键盘显示的情况下,此时如果要切换显示出表情面板,由于表情面板不可能和用户的软键盘高度恰好一样,此外由于控件的上下移位,就会出现表情面板的跳闪现象
在点击切换按钮的时候,表情面板会先向上跳,然后再往下移,这样就会带来很差的用户体验,效果如下图所示:
这里提供一个解决方案,使软键盘和表情面板可以很自然地切换,效果如下图所示:
解决思路主要是这样:系统在弹出软键盘时,会将内容输入框view以及其之上的view都给顶上去,当切换到表情面板时,只有将表情面板的高度保持为和软键盘的高度一致,才能自然地切换。此外,还需要将内容输入框view以及其之上的view的位置固定住,这样才不会出现跳闪问题
主要的操作逻辑都在 emojikeyboard 类中
/** * 作者: chenzy * 时间: 2017/8/26 18:12 * 描述: */ 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(); } /** * 如果之前没有保存过键盘高度值 * 则在进入activity时自动打开键盘,并把高度值保存下来 */ 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; } /** * 锁定内容view以防止跳闪 */ private void lockcontentviewheight() { linearlayout.layoutparams layoutparams = (linearlayout.layoutparams) contentview.getlayoutparams(); layoutparams.height = contentview.getheight(); layoutparams.weight = 0; } /** * 释放锁定的内容view */ 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; } }
这里也提供代码下载:android 解决表情面板和软键盘切换时跳闪的问题
总结
以上所述是小编给大家介绍的android 表情面板和软键盘切换时跳闪问题的解决方法,希望对大家有所帮助