android表情编码与解码

很多时候我们和服务器交互或者前端将文本保存到文件的时候,涉及一些带有表情符号的文本,因为存储的原因,要进行编码和解码过程,否则我们可能无法取出或者存入,或者是乱码。(一个简单的例子)

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    tools:context="test.com.unicodet.MainActivity">

    <EditText
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/control"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="显示"
        android:textSize="15sp"
        android:background="#00aa00"/>
    <TextView
        android:id="@+id/display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginTop="20dp" />
</LinearLayout>

Activity

//  Android表情符号的输入与取出
public class MainActivity extends AppCompatActivity{

    private EditText mInput;
    private Button mControl;
    private TextView mDisplay;
    private String mContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initClick();
    }

    /**
     *初始化控件
     */
    private void initView() {
        mInput= (EditText) findViewById(R.id.input);
        mControl= (Button) findViewById(R.id.control);
        mDisplay= (TextView) findViewById(R.id.display);
    }

    /**
     * 点击事件
     */
    private void initClick() {
        mInput.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) {
                mContent=encode(s.toString());
                Log.e("TAG","---------"+mContent);
            }
        });
        //显示
        mControl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDisplay.setText(decode(mContent));
            }
        });
    }

    /**
     * 将输入的内容编码
     * @param content
     * @return
     */
    public static String encode(String content) {
        StringBuilder sb = new StringBuilder(content.length() * 3);
        for (char c : content.toCharArray()) {
            if (c < 256) {
                sb.append(c);
            } else {
                sb.append("\\u");
                sb.append(Character.forDigit((c >>> 12) & 0xf, 16));
                sb.append(Character.forDigit((c >>> 8) & 0xf, 16));
                sb.append(Character.forDigit((c >>> 4) & 0xf, 16));
                sb.append(Character.forDigit((c) & 0xf, 16));
            }
        }
        return sb.toString();
    }

    /**
     * 将取出内容解码
     * @param content
     * @return
     */
    public static String decode(String content) {
        final Pattern reUnicode = Pattern.compile("\\\\u([0-9a-zA-Z]{4})");
        Matcher sMatcher = reUnicode.matcher(content);
        StringBuffer sb = new StringBuffer(content.length());
        while (sMatcher.find()) {
            sMatcher.appendReplacement(sb,
                    Character.toString((char) Integer.parseInt(sMatcher.group(1), 16)));
        }
        sMatcher.appendTail(sb);
        return sb.toString();
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值