Android TextView、EditText显示输入法自带表情,记录一下

参考文章写在首位

Android emoji显示
嘿嘿,其实代码都没有变。

在Android系统中使用系统自带的emoji表情

这篇文章里介绍,emoji表情实际上是一组Unicode编码与一组表情描述之间的一一对应。,至于对应什么图片,就看不同系统怎么设定了。

TextView和EditText本身就已经支持表情了,只不过将emoji发给后台,后台再发回来会出现问题。

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:id="@+id/et"
        android:layout_width="0dp"
        android:layout_height="50dp"
        app:layout_constraintTop_toBottomOf="@+id/tv"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />

</android.support.constraint.ConstraintLayout>

一个TextView用来显示EditText输入的内容。

Activity代码

public class MainActivity extends AppCompatActivity {

    TextView tv;
    EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);
        et = findViewById(R.id.et);
        //输入的内容转为Unicode编码后
        String str = "\\ud83d\\ude1a\\u5192\\u54af\\u51d1\\u697c\\u4e0b\\u4e86\\u5973\\u5b69\\ud83d\\ude1c\\ud83d\\ude1a";

        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = et.getText().toString();
                //将输入的内容转Unicode
                String unicode = string2Unicode(str);
                //再将Unicode转为String,直接设置给TextView
                tv.setText(unicode2String(unicode));
            }
        });

    }

    /**
     * unicode 转字符串
     * ---------------------
     * 作者:子不语归来
     * 来源:CSDN
     * 原文:https://blog.csdn.net/u010123643/article/details/54019448?utm_source=copy
     */
    public static String unicode2String(String unicode) {
        Log.e("str==", "111111111" + "\\\\u");
        StringBuffer string = new StringBuffer();

        String[] hex = unicode.split("\\\\u");

        for (int i = 1; i < hex.length; i++) {
            Log.e("str==", "22222222" + "\\\\u");
            // 转换出每一个代码点
            int data = Integer.parseInt(hex[i], 16);

            // 追加成string
            string.append((char) data);
        }

        return string.toString();
    }

    /**
     * 字符串转换unicode
     * ---------------------
     * 作者:子不语归来
     * 来源:CSDN
     * 原文:https://blog.csdn.net/u010123643/article/details/54019448?utm_source=copy
     */
    public static String string2Unicode(String string) {

        StringBuffer unicode = new StringBuffer();

        for (int i = 0; i < string.length(); i++) {

            // 取出每一个字符
            char c = string.charAt(i);
            if (c < 256)//ASC11表中的字符码值不够4位,补00
            {
                unicode.append("\\u00");
            } else {
                unicode.append("\\u");
            }
            // 转换为unicode
            unicode.append(Integer.toHexString(c));
        }

        return unicode.toString();
    }
}

直接Log,出的表情是乱码,这边就先转成Unicode字符串,然后再转回去,设置给TextView。不然发给后台也是乱码,如果是文字加上表情,还是要和后台进行沟通,不然发过去的全是Unicode之后的字符串,又是麻烦。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值