简体   繁体   English

fromHtml:JellyBean和KitKat(及更高版本)上的行为不同

[英]fromHtml: Different behaviour on JellyBean and KitKat (and above)

I am using fromHtml to display formatted text (bold italic etc) in TextView . 我正在使用fromHtmlTextView显示格式化的文本(粗体斜体等)。 However, I found it's behaviour is different on JellyBean (4.1.2) and KitKat(4.4.2) 但是,我发现它在JellyBean(4.1.2)和KitKat(4.4.2)上的行为不同

Here is code: 这是代码:

    String myHtml = "<b>hello</b>&#128516;";
    Spanned spanned = Html.fromHtml(myHtml, null, null);

Here html string has &#128516; 此处的html字符串具有&#128516; which is unicode for an emoji. 这是表情符号的unicode。 Now after calling fromHtml it returns following value on KitKat (and above): 现在,在从fromHtml调用fromHtml它将在KitKat(及更高版本)上返回以下值:

spanned = hello😄

Here is screenshot of Android Studio for the same: 这是相同的Android Studio屏幕截图:

在此处输入图片说明

This is expected behaviour as we can see corresponding emoji in spanned . 这是正常现象,因为我们可以看到相应的表情符号spanned

But on JellyBean the same call returns following value: 但是在JellyBean上,相同的调用返回以下值:

spanned = hello�� 

Here is screenshot: 这是屏幕截图:

在此处输入图片说明

This is indeed not expected and driving me nuts. 这确实是意料之外的,这让我发疯。 I don't know what I am doing wrong. 我不知道我在做什么错。 If anyone having idea please can you help? 如果有人有想法,请您能帮忙吗?

add this java file in your src and add this font .ttf file in assets dir 在您的src添加 java文件,并在assets dir添加 font .ttf文件

now use this like below 现在像下面这样使用

String myHtml = "<b>hello</b>&#128516;";
Spanned spanned = AndroidEmoji.ensure(myHtml);

for more check here gitcode. 有关更多信息,请参见gitcode。

Surprisingly, root of this problem was in Html.toHtml which I had used to convert text in TextView to html. 出乎意料的是,此问题的根源是我用来将TextView文本转换为html的Html.toHtml I used custom toHtml and this problem solved. 我使用自定义toHtml并解决了这个问题。 I used .toHtml written in this answer. 我用这个答案中写的.toHtml Indeed it is great solution. 确实,这是一个很好的解决方案。 I wonder how Android's original Html.toHtml is so lame and defective. 我不知道Android的原始Html.toHtml如何如此Html.toHtml和有缺陷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 setLooping(true)在豆形软糖和奇巧中不起作用 - setLooping(true) is not working in jellybean and kitkat kitkat和豆形软糖设备中的项目编译问题 - project compiling problems in kitkat and jellybean device 强制关闭kitkat和棒棒糖的活动,但在豆形软糖中起作用? - force close an activity in kitkat & lollipop but work in jellybean? GPS程序适用于JellyBean和KitKat,但不适用于Gingerbread - GPS program works on JellyBean and KitKat but not Gingerbread 将应用程序从软糖版本扩展到Kitkat进行SMS中止广播 - Extending the app from the Jellybean version to the Kitkat for SMS abort broadcast FacebookSDK Request.newMeRequest onCompleted在JELLYBEAN上被调用,但不是KITKAT或LOLLIPOP - FacebookSDK Request.newMeRequest onCompleted called on JELLYBEAN but not KITKAT or LOLLIPOP WebKit上方的Web View水平滚动问题 - Web View Horizontal Scrolling issues above kitkat Geocoder在kitkat以上版本的android中不起作用 - Geocoder is not working in android above kitkat version 如何以编程方式在Android(KitKat及更高版本)中添加APN - How to add APN in Android (KitKat and above) programmatically 使 Android 应用程序兼容 Kitkat 及以上版本 - Making android app compatible with Kitkat and above versions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM