【问题标题】:How to make transition between emoji keyboard and soft keyboard?如何在表情符号键盘和软键盘之间进行转换?
【发布时间】:2016-01-20 06:05:42
【问题描述】:

我正在开发一个 android 即时聊天应用程序。我已经成功实现了它的基本功能。现在,我使用库 compile 'com.rockerhieu.emojicon:library:1.3.3' 添加了一组表情符号。我在我的 XML 文件中使用 FrameLayout 来显示表情符号。

1. activity_chat.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FCAB26"
    android:orientation="vertical"
    android:weightSum="1">

    <ListView
        android:id="@+id/list_view_messages"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".60"
        android:background="@null"
        android:divider="@null"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"></ListView>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight=".10"
        android:orientation="horizontal"
        android:weightSum="1">

        <ImageView
            android:id="@+id/imgSmile"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".10"
            android:src="@drawable/ic_msg_panel_smiles"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="-10sp"/>

        <com.rockerhieu.emojicon.EmojiconEditText
            android:id="@+id/edtMessage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:hint="Enter Message"
            android:layout_weight=".60"></com.rockerhieu.emojicon.EmojiconEditText>

        <Button
            android:id="@+id/btnSendMessage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight=".30"
            android:gravity="center"
            android:onClick="onClick"
            android:text="Send Message" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/emojicons"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".30"
        android:visibility="gone" />

</LinearLayout>

即使在将可见性指定为消失后,FrameLayout 也会占用一些空间。

通过上述库显示表情面板的代码如下:

 // This method will set a panel of emoticons in the fragment
private void setEmojiconFragment(boolean useSystemDefault) {
    // Replacing the existing fragment having id emojicons with the fragment of emoticons library containing emoticons
    getSupportFragmentManager().beginTransaction().replace(R.id.emojicons, EmojiconsFragment.newInstance(useSystemDefault)).commit();
}

3.截图

正如您在屏幕截图中看到的,我有一个 ImageView、一个 EditText 和发送消息按钮。单击 ImageView 时,将显示 emoji 面板,单击 EditText 时隐藏 emoji 键盘并显示软键盘。以下代码处理此问题。

显示表情符号弹出窗口:

public void showEmojiPopUp(boolean showEmoji) {
    FrameLayout frameLayout = (FrameLayout) findViewById(R.id.emojicons);
    frameLayout.setVisibility(View.VISIBLE);
}

隐藏软键盘:

public void hideKeyboard() {
        InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

我的问题是我希望表情符号键盘和软键盘占据相同的空间。一次只能看到一个。我想在表情符号键盘和软键盘之间进行转换。正如您在代码中看到的,我在 FrameLayout 中添加了表情符号键盘。我无法在这里使软键盘和表情符号键盘的大小相等。请帮我解决问题。

编辑代码: 我对我的代码进行了一些更改:

showEmojiPopUp() 方法:这里我们调整 FrameLayout 包含表情符号面板的高度,使 softkeyboard 和表情符号键盘的高度相同。

 public void showEmojiPopUp(boolean showEmoji) {
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    deviceHeight = size.y;
    Log.e("Device Height", String.valueOf(deviceHeight));
    frameLayout = (FrameLayout) findViewById(R.id.emojicons);
    frameLayout.getLayoutParams().height = (int) (deviceHeight / 2.5); // Setting the height of FrameLayout
    frameLayout.requestLayout();
    frameLayout.setVisibility(View.VISIBLE);
    hideKeyboard();
}

2。 hideKryboard() 方法

// Hiding the keyboard
public void hideKeyboard() {
    InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

它现在对我有用。

【问题讨论】:

  • 嗨...您找到任何解决方案了吗?我有同样的问题
  • 请告诉我它是否适合你。
  • 好吧,我使用了一些不同的代码......但逻辑是一样的
  • @HRaval:你能将表情符号发送到不同的设备吗?我正在使用 String toServer = StringEscapeUtils.escapeJava(message); 将表情符号编码为 un​​icode 字符;我正在使用 String messageReceived = StringEscapeUtils.unescapeJava(unicodeMessageReceived);现在我通过 EmojiconTextView 显示 unicode。但它没有将 unicode 转换为相应的图像。你能帮我修复它吗?

标签: android emoji soft-keyboard


【解决方案1】:

我正在使用从这里找到的这段代码....link

 public Spannable parse(String text) {
        CharSequence fromCache = cache.get(text);
        if (fromCache != null) {
            return (Spannable) fromCache;
        } else {
            CharSequence parsed = emoji.replaceEmoji(text);
            Matcher matcher = userReference.matcher(text);
            Spannable s;
            if (parsed instanceof Spannable) {
                s = (Spannable) parsed;
            } else {
                s = Spannable.Factory.getInstance().newSpannable(parsed);
            }

            while (matcher.find()) {
                s.setSpan(new ForegroundColorSpan(0xff427ab0), matcher.start(), matcher.end(), 0);
            }
            cache.put(text, s);

            return s;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-23
    • 2021-05-29
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2011-10-13
    相关资源
    最近更新 更多