文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android 表情面板和软键盘切换时跳闪问题的解决方法

2023-05-30 21:56

关注

现在很多应用都会在让用户输入各种文本信息的时候同时多提供一个表情面板,这样就会出现一个问题,即表情面板的跳闪问题要输入文本信息,那固然是需要弹出软键盘,在软键盘显示的情况下,此时如果要切换显示出表情面板,由于表情面板不可能和用户的软键盘高度恰好一样,此外由于控件的上下移位,就会出现表情面板的跳闪现象

在点击切换按钮的时候,表情面板会先向上跳,然后再往下移,这样就会带来很差的用户体验,效果如下图所示:

Android 表情面板和软键盘切换时跳闪问题的解决方法

这里提供一个解决方案,使软键盘和表情面板可以很自然地切换,效果如下图所示:

Android 表情面板和软键盘切换时跳闪问题的解决方法

解决思路主要是这样:系统在弹出软键盘时,会将内容输入框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

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 资料下载
  • 历年真题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯