M.H*_*adi 3 java android emoji numberformatexception
我尝试从数据库中获取长字符串内的表情符号代码,格式如下:0x1F60A ...所以我可以访问代码,但它将是一个String。
起初,我尝试通过执行以下操作来转换变量tv.setText(beforeEmo + getEmijoByUnicode((int)emoKind));,但 Android Studio 提示:“无法将 'java.lang.String' 转换为 int”...
getEmijoByUnicode方法是:
public String getEmijoByUnicode(int unicode) {
return new String(Character.toChars(unicode));
}
Run Code Online (Sandbox Code Playgroud)
所以我尝试了这个:
tv.setText(beforeEmo + getEmijoByUnicode(Integer.parseInt(emoKind)));
Run Code Online (Sandbox Code Playgroud)
但它因 NumberFormatError 崩溃。有什么办法可以让表情符号出现在我的文本中吗?
尝试
Integer.parseInt("1F60A", 16);
Run Code Online (Sandbox Code Playgroud)
或者
Long.parseLong("1F60A", 16);
Run Code Online (Sandbox Code Playgroud)
将字符串转换为 int 或 long。所以你必须摆脱“0x”,就像这样
getEmijoByUnicode(Integer.parseInt(emoKind.substring(2), 16));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
791 次 |
| 最近记录: |