【问题标题】:Android replace keyboard with Emoji FragmentAndroid用表情符号片段替换键盘
【发布时间】:2014-05-11 17:53:57
【问题描述】:

我正在我的应用程序中进行一些聊天,并且我有一个表情符号片段(就像来自 whatsapp 或电报的那个)。如何在没有任何奇怪动画的情况下在片段和键盘之间切换?

我已经有了带有表情符号和自定义 EditText 的片段。我只想在那个片段和键盘之间切换。我真的希望它像 whatsapp 或 Telegram 一样工作。

我为表情符号片段制作了一个库。我在与 EditText 相同的布局中添加了一个片段(每个表情符号带有 SpannableTextViews 的网格视图)。

任何帮助将不胜感激。

【问题讨论】:

    标签: android keyboard switch-statement emoji emoticons


    【解决方案1】:

    您不需要更换键盘,您可以使用PopupWindow 将片段置于活动之上,就像Telegram 一样。看看source:方法showEmojiPopup创建EmojiView并把它放在PopupWindow里面然后计算出合适的大小并显示出来。

    emojiPopup.setHeight(View.MeasureSpec.makeMeasureSpec(currentHeight, View.MeasureSpec.EXACTLY));
    emojiPopup.setWidth(View.MeasureSpec.makeMeasureSpec(contentView.getWidth(), View.MeasureSpec.EXACTLY));
    
    emojiPopup.showAtLocation(parentActivity.getWindow().getDecorView(), 83, 0, 0);
    

    【讨论】:

    • 感谢您的帮助。这正是我想要的。
    • @DavidStanete 不客气。不要忘记接受答案。谢谢。
    • 什么是 parentActivity 解释一下
    • @Foxinsocks 您的答案中 currentHeight 的值是多少?
    • @gikarasojokinene 我想的键盘高度。查看来源。
    【解决方案2】:

    你需要编码:

    public class EmojiKeyboard {
    
        private static final String TAG = "EmojiKeyboard";
        private static final String PREF_KEY_HEIGHT_KB = "EmojiKbHeight";
    
        private Context context;
        private int screenHeight = -1;
        private int emojiKbHeight = -1;
        private PopupWindow emojiKeyboardPopup;
        private View view;
        private SharedPreferences preferences;
    
        public EmojiKeyboard(Context context, View view) {
            if (context instanceof Activity) {
                this.context = context;
                this.view = view;
                preferences = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE);
    
                //Restore EmojiKeyboard Height
                emojiKbHeight = preferences.getInt(PREF_KEY_HEIGHT_KB, -1);
    
                //TODO support less then 11 API, and not perfect resizing when switched the keyboard
                view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                    @Override
                    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                        /*
                        * Get root view height
                        * */
                        screenHeight = screenHeight == -1 && bottom > oldBottom
                                ? bottom
                                : screenHeight;
    
                        /*
                        * Calculate soft keyboard height
                        * */
                        int dHeight = oldBottom - bottom;
                        boolean validHeight = emojiKbHeight == -1 && dHeight > 80 && bottom != oldBottom;
    
                        /*
                        * Сheck twice because the keyboard may have been switched
                        * */
                        emojiKbHeight = validHeight
                                ? dHeight : emojiKbHeight != (dHeight) && dHeight > 0
                                ? dHeight
                                : emojiKbHeight;
    
                        /*
                        * Store emoji keyboard height into SharedPreferences
                        * */
                        preferences.edit().putInt(PREF_KEY_HEIGHT_KB, emojiKbHeight).commit();
    
                        /*
                        * If layout returned to a standard height then dismissing keyboard (OnBackPressed)
                        * */
                        if (screenHeight == bottom) {
                            dismissEmojiKeyboard();
                        }
    
                        /*
                        * Resize emoji on the go when a user switches between keyboards
                        * */
                        resizeEmoji();
                    }
                });
            }
        }
    
    
        public void showEmoji() {
            if (emojiKeyboardPopup == null) {
                createEmojiKeyboard();
            }
            if (!isShowed()) {
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        emojiKeyboardPopup.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                        resizeEmoji();
                    }
                }, 10L);
    
            } else {
                dismissEmojiKeyboard();
            }
        }
    
        public void createEmojiKeyboard() {
            EmojiView emojiKeyboard = new EmojiView(context, EmojiView.EMOJI_DARK_STYLE, new EmojiView.onEmojiClickListener() {
                public void onBackspace() {
                    if (((Activity) context).getWindow().getCurrentFocus() instanceof EditText) {
                        ((Activity) context).getWindow().getCurrentFocus().dispatchKeyEvent(new KeyEvent(0, 67));
                    }
                }
    
                public void onEmojiSelected(Emojicon emojicon) {
                    if (((Activity) context).getWindow().getCurrentFocus() instanceof EditText) {
                        EmojiView.input((EditText) ((Activity) context).getWindow().getCurrentFocus(), emojicon);
                    }
                }
            });
            emojiKeyboardPopup = new PopupWindow(emojiKeyboard);
            emojiKeyboardPopup.setHeight(View.MeasureSpec.makeMeasureSpec(setEmojiKeyboardHeight(), View.MeasureSpec.EXACTLY));
            emojiKeyboardPopup.setWidth(View.MeasureSpec.makeMeasureSpec(getDisplayDimensions(context).x, View.MeasureSpec.EXACTLY));
            emojiKeyboardPopup.setAnimationStyle(0);
        }
    
        public void dismissEmojiKeyboard() {
            if (isShowed()) {
                emojiKeyboardPopup.dismiss();
            }
        }
    
        public boolean isShowed() {
            return emojiKeyboardPopup != null && emojiKeyboardPopup.isShowing();
        }
    
        /*
        * Emoji set up size
        * */
        public void resizeEmoji() {
            if (isShowed()) {
                WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) emojiKeyboardPopup.getContentView().getLayoutParams();
                layoutParams.height = setEmojiKeyboardHeight();
                wm.updateViewLayout(emojiKeyboardPopup.getContentView(), layoutParams);
            }
        }
    
        public int setEmojiKeyboardHeight() {
            return emojiKbHeight == -1 && emojiKbHeight != screenHeight && emojiKbHeight < 80
                    ? (getDisplayDimensions(context).y / 2)
                    : emojiKbHeight;
        }
    
        public Point getDisplayDimensions(Context context) {
            Point size = new Point();
            WindowManager w = ((Activity) context).getWindowManager();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                w.getDefaultDisplay().getSize(size);
            } else {
                Display d = w.getDefaultDisplay();
                size.x = d.getWidth();
                size.y = d.getHeight();
            }
            return size;
        }
    }
    

    解决问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-29
      • 2017-09-11
      • 2021-05-29
      • 2015-10-16
      • 2014-04-20
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多