Android заменил клавиатуру на Emoji Fragment

Я делаю небольшой чат в своем приложении, и у меня есть фрагмент смайлика (точно такой же, как в WhatsApp или Telegram). Как я могу переключаться между фрагментом и клавиатурой без какой-либо странной анимации?

У меня уже есть фрагмент с смайликами и пользовательским EditText. Я просто хочу переключиться между этим фрагментом и клавиатурой. Я очень хочу, чтобы он работал как WhatsApp или Telegram.

Для фрагмента смайлика я сделал библиотеку. Я добавляю фрагмент (представление сетки с SpannableTextViews для каждого смайлика) в том же макете, что и EditText.

Любая помощь будет действительно оценена.


person stanete    schedule 11.05.2014    source источник


Ответы (2)


Вам не нужно заменять клавиатуру, вы можете поместить свой фрагмент поверх активности, используя PopupWindow как это делает Telegram. Просто посмотрите на источник: метод 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);
person eleven    schedule 11.05.2014
comment
Спасибо за помощь. Это именно то, что я искал. - person stanete; 12.05.2014
comment
@DavidStanet Пожалуйста. Не забудьте принять ответ. Спасибо. - person eleven; 12.05.2014
comment
что такое parentActivity объясните это - person Sonu Kumar; 02.11.2015
comment
@Foxinsocks, каково значение currentHeight в вашем ответе? - person Edijae Crusar; 22.01.2016
comment
@gikarasojokinene высота клавиатуры, я полагаю. Посмотрите на источники. - person eleven; 22.01.2016
comment
Кажется, что клавиатура скрыта, когда Telegram отобразил фрагмент смайлика. Почему размер макета активности не изменяется, чтобы заполнить всю высоту экрана? - person Petrakeas; 24.02.2016
comment
(1) showEmojiPopup больше не существует; (2) 83 не является флагом Gravity (где вы его взяли?); (3) это не хочет опускаться ниже клавиатуры. Нижняя часть всегда выравнивается с верхней частью клавиатуры. - person AutonomousApps; 26.01.2018

Вам нужно закодировать:

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

Решить проблему

person Sonu Kumar    schedule 02.11.2015