2016-12-29 65 views
2

表情符号,我已经开发了一个键盘,现在我需要表情符号吧,其他的问题我已经意识到最好的办法是与popupwindow,实现Android键盘与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需要一个视图作为其第一个变种,如果键盘是另一个应用程序,我没有一个视图给他......

有没有办法解决这个问题? 或者我是否全都错了,还有更好的方法...

所有帮助将不胜感激!

+0

试试这个[link](https://github.com/vanniktech/Emoji/blob/master/app/src/main/res/layout/adapter_chat.xml) –

+0

不起作用,它只适用于应用程序,我试图在键盘上实现它,所以我没有rootView – yanivtwin

回答

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

+0

试试这个[链接](https://www.numetriclabz.com/how-to-integrate-emoticons-using-emojicon-library-in -android /) –

0

可以参考的完整代码

EmojiIcon

+0

好吧,我正在尝试使用它,并有一个片段,将钩住我,但我如何从键盘服务调用它?我instanciated它,但仍然需要启动它以某种方式从键盘 – yanivtwin

0

您好,我已经做了同样的事情,这GitHub的链接。我在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

谢谢,希望这会帮助你。它有点老,但你可以尝试。

+0

这个例子链接显示一个活动,这将工作在键盘服务?它似乎并不如此 – yanivtwin

+0

在活动中,我做了自定义键盘检查它。我已经评论了代码。 – Saveen