添加从带有表情符号的弹出窗口 window/menu 中选择的文本和表情符号(字符)

Add text and emoticon (characters) that are selected from a popup window/menu with emoticons

在我的应用程序中,我想输入带有常规字符和表情符号快捷方式的文本。

在从软键盘输入字符时,我想启动一个弹出菜单或弹出菜单 window,其中包含一系列 icons/emoticon。按 icon/emoticon 将在光标处插入表情符号快捷字符。

可以有普通字符和表情符号快捷字符的文本。在文本中显示表情符号是更进一步。

我喜欢的解决方案是:

1 - 单击笑脸按钮后,我会启动一个弹出窗口:

@Override
public void onClick(View v) {
    switch( v.getId()) {
    case R.id.fragment_log_popup_smileys:
        PopupIcons popup = new PopupIcons( myActivity, new PopupIconResultHandler() {
            @Override
            public void iconClicked( String iconResult) {
                logTextV.getText().insert( logTextV.getSelectionStart(), iconResult);
            }
         });
        popup.show();
        break;

文本中插入了任何笑脸字符。它们可以显示为相同的笑脸。这样做很容易。

2 - 弹出窗口

public class PopupIcons implements Serializable {
    Activity myActivity; 
    PopupIconResultHandler myClickHandler; 
    public PopupIcons( final Activity activityContext, PopupIconResultHandler clickHandler) { 
    myActivity = activityContext;
    myClickHandler = clickHandler;
}
public void show() {
    Rect rectgle= new Rect();
    Window window= myActivity.getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame( rectgle);
    int StatusBarHeight= rectgle.top;
    int contentViewTop= window.findViewById( Window.ID_ANDROID_CONTENT).getTop();
    int TitleBarHeight= contentViewTop - StatusBarHeight;
    Display display = ((WindowManager) myActivity.getSystemService( Context.WINDOW_SERVICE)).getDefaultDisplay();
    LinearLayout viewGroup = (LinearLayout) myActivity.findViewById( R.id.popupIconsLinearLayout);
    LayoutInflater layoutInflater = (LayoutInflater) myActivity.getSystemService( Context.LAYOUT_INFLATER_SERVICE);
    View layout = layoutInflater.inflate( R.layout.popup_icons, viewGroup);

    // Creating the PopupWindow
    final PopupWindow popup = new PopupWindow( myActivity);
    popup.setContentView(layout);
    popup.setFocusable(true);
    popup.setAnimationStyle( R.style.PopupMenu);
    popup.setWidth( FrameLayout.LayoutParams.WRAP_CONTENT);  
    popup.setHeight(FrameLayout.LayoutParams.WRAP_CONTENT);

    View.OnClickListener handler = new View.OnClickListener() {
        @Override
        public void onClick( View v) {
            if( myClickHandler != null) {
                myClickHandler.iconClicked( (String) v.getTag()); 
            }
            popup.dismiss(); 
        }
      };

    ImageButton but = (ImageButton) layout.findViewById( R.id.popup_icon_smile); but.setOnClickListener( handler);
    but = (ImageButton) layout.findViewById( R.id.popup_icon_smile_big); but.setOnClickListener( handler);
    but = (ImageButton) layout.findViewById( R.id.popup_icon_smile_cool); but.setOnClickListener( handler);
    // etc ... for all buttons

    popup.showAtLocation( layout, Gravity.BOTTOM | Gravity.LEFT, 30 , 30);   
}

}

我喜欢这种方法。 您有改进建议吗?