使用popupwindow在Android键盘中实现表情符号

时间:2016-12-29 11:12:05

标签: android android-service android-softkeyboard

我开发了一个键盘,现在我需要添加表情符号,从其他问题我已经意识到最好的方法是使用popupwindow,

这就是我所做的:

  case -102:
            LayoutInflater layoutInflater
                    = (LayoutInflater)getBaseContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.emoji_view, null);
            final PopupWindow popupWindow = new PopupWindow(
                    popupView,
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            popupWindow.showAsDropDown(getWindow().getOwnerActivity().getCurrentFocus(),50, -30);

不幸的是,这不起作用,showAsDropDown需要一个视图作为它的第一个var,如果键盘在另一个应用程序中,我没有视图给他...

有没有办法解决这个问题? 或者我认为这一切都错了,有更好的方法......

所有帮助将不胜感激!

3 个答案:

答案 0 :(得分:2)

鉴于您使用官方SoftKeyBoard implementation作为键盘模板:

//Cut some pieces of the code for clarity
case -102:
    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
        .getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.emoji_view, null);
    PopupWindow popupWindow = new PopupWindow(popupView, MATCH_PARENT, MATCH_PARENT);
    popupWindow.showAsDropDown(mInputView);

使用您为键盘充气的视图,在正式的SoftKeyBoard中,其上方的代码段称为mInputView

答案 1 :(得分:1)

嗨,我做了同样的事情。我在android中制作了一个自定义键盘。

EmoticonsPagerAdapter emojiAdapter;

/**
 * Defining all components of emoticons keyboard
 */
private void enablePopUpView() {

    final ViewPager pager = (ViewPager) popUpView
            .findViewById(R.id.emoticons_pager);
    pager.setOffscreenPageLimit(3);

    final ArrayList<EmojiItem> paths = EmojiUtil.getInstance(acitiviy)
            .getAllEmojis();
    final ArrayList<EmojiItem>[] groups = new ArrayList[5];
    for (EmojiItem emoji : paths) {
        if (groups[emoji.emojiGroup] == null) {
            groups[emoji.emojiGroup] = new ArrayList<EmojiItem>();
        }
        groups[emoji.emojiGroup].add(emoji);
    }
    final ArrayList<EmojiItem> history = new ArrayList<EmojiItem>();
    ArrayList<Integer> historyIds = SettingsUtil.getHistoryItems(acitiviy);
    for (Integer his : historyIds) {
        for (EmojiItem emoji : paths) {
            if (emoji.id == his) {
                history.add(emoji);
                break;
            }
        }
    }
    history.add(paths.get(0));

    final KeyClickListener onEmojiClick = new KeyClickListener() {

        @Override
        public void keyClickedIndex(EmojiItem index) {

            int cursorPosition = editMessage.getSelectionStart();
            editMessage.getText().insert(cursorPosition, index.emojiText);
            try {
                editMessage.getText().setSpan(
                        new ImageSpan(index.emojiDrawable), cursorPosition,
                        cursorPosition + 1,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } catch (Exception e) {
            }
            if (history.get(0) != index)
                history.add(0, index);
            SettingsUtil.setHistoryItems(acitiviy, history);
            emojiAdapter.notifyDataSetChanged();
            pager.setAdapter(emojiAdapter);
        }
    };

    ((ImageButton) popUpView.findViewById(R.id.emoji2))
            .setImageDrawable(groups[0].get(0).emojiDrawable);
    ((ImageButton) popUpView.findViewById(R.id.emoji3))
            .setImageDrawable(groups[1].get(0).emojiDrawable);
    ((ImageButton) popUpView.findViewById(R.id.emoji4))
            .setImageDrawable(groups[2].get(0).emojiDrawable);
    ((ImageButton) popUpView.findViewById(R.id.emoji5))
            .setImageDrawable(groups[3].get(0).emojiDrawable);
    ((ImageButton) popUpView.findViewById(R.id.emoji6))
            .setImageDrawable(groups[4].get(0).emojiDrawable);
    popUpView.findViewById(R.id.emoji1).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    emojiAdapter.emojis = history;
                    emojiAdapter.notifyDataSetChanged();
                    pager.setAdapter(emojiAdapter);
                }
            });
    popUpView.findViewById(R.id.emoji2).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    emojiAdapter.emojis = groups[0];
                    emojiAdapter.notifyDataSetChanged();
                    pager.setAdapter(emojiAdapter);
                }
            });
    popUpView.findViewById(R.id.emoji3).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    emojiAdapter.emojis = groups[1];
                    emojiAdapter.notifyDataSetChanged();
                    pager.setAdapter(emojiAdapter);
                }
            });
    popUpView.findViewById(R.id.emoji4).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    emojiAdapter.emojis = groups[2];
                    emojiAdapter.notifyDataSetChanged();
                    pager.setAdapter(emojiAdapter);
                }
            });
    popUpView.findViewById(R.id.emoji5).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    emojiAdapter.emojis = groups[3];
                    emojiAdapter.notifyDataSetChanged();
                    pager.setAdapter(emojiAdapter);
                }
            });
    popUpView.findViewById(R.id.emoji6).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    emojiAdapter.emojis = groups[4];
                    emojiAdapter.notifyDataSetChanged();
                    pager.setAdapter(emojiAdapter);
                }
            });

    emojiAdapter = new EmoticonsPagerAdapter(acitiviy, groups[0],
            onEmojiClick);
    pager.setAdapter(emojiAdapter);

    // Creating a pop window for emoticons keyboard
    popupWindow = new PopupWindow(popUpView, LayoutParams.MATCH_PARENT,
            (int) keyboardHeight, false);

    View backSpace = (View) popUpView.findViewById(R.id.imageBackspace);
    backSpace.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0,
                    0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
            editMessage.dispatchKeyEvent(event);
        }
    });

    popupWindow.setOnDismissListener(new OnDismissListener() {

        @Override
        public void onDismiss() {
            emoticonsCover.setVisibility(LinearLayout.GONE);
        }
    });

    ViewPager pagerStickers = (ViewPager) popUpView
            .findViewById(R.id.stickers_pager);
    pagerStickers.setOffscreenPageLimit(3);

}

private void showKeyboardPopup(View root, boolean attaches) {
    if (!popupWindow.isShowing()) {
        popupWindow.setHeight((int) (keyboardHeight));

        if (isKeyBoardVisible) {
            imageEmoji.setImageResource(R.drawable.emoji_kbd);
            emoticonsCover.setVisibility(LinearLayout.GONE);

        } else {
            imageEmoji.setImageResource(R.drawable.ic_down);
            emoticonsCover.setVisibility(LinearLayout.VISIBLE);
        }
        try {
            popupWindow.showAtLocation(root, Gravity.BOTTOM, 0, 0);
        } catch (Exception e) {
        }
    } else {
        imageEmoji.setImageResource(R.drawable.emoji_btn_normal);
        popupWindow.dismiss();
        return;
    }

    imageAttaches.setBackgroundColor(attaches ? 0xFF808080 : 0x00000000);
    imageEmojis.setBackgroundColor(attaches ? 0x00000000 : 0xFF808080);
    imageStickers.setBackgroundColor(0x00000000);
    layoutEmojis.setVisibility(attaches ? View.GONE : View.VISIBLE);
    layoutStickers.setVisibility(View.GONE);

}

请查看详细信息,点击here

谢谢希望这会对你有所帮助。它有点旧,但你可以尝试一下。

答案 2 :(得分:0)

您可以参考此github链接获取完整代码

EmojiIcon