2016-12-29 10 views
2

とアンドロイドキーボードで絵文字を実装Iをキーボードを開発し、今私は最善の方法はpopupwindowである実現してきた他の質問から、それに絵文字を追加する必要があり、popupwindow

は、ここに私がきたものだしました行わ:残念ながら、これは動作しません、showAsDropDownは、その最初のVARとしてビューを必要とし、キーボードが別のアプリであれば、私は彼を与えるためのビューを持っていない

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); 

...

これを修正する方法はありますか? またはそれについてすべて間違っているといい方法があります...

すべてのお役に立ちます!

//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

このリンクを試す –

+0

はアプリケーションでのみ動作しますが、私はキーボードに実装しようとしているので、rootViewはありません – yanivtwin

答えて

2

はあなたのキーボードのためのテンプレートとして公式SoftKeyBoard implementationを使用していることを考えます。

+0

この[リンク] -android /) –

0

あなたがhi私は同じことを行っている完全なコードのために

EmojiIcon

+0

私はそれを使用しようとしていますが、私に繋がる断片がありますが、キーボードサービスからどのように呼び出すのですか?私はそれをinstanciatedしかし、それでもキーボードから何とかそれを開始する必要があります – yanivtwin

0

このgithubのリンクを参照することができます。私はアンドロイドでカスタムキーボードを作った。

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