Android WebView UTF-8 未显示 [英] Android WebView UTF-8 not showing

查看:38
本文介绍了Android WebView UTF-8 未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 webview,正在尝试将简单的 UTF-8 文本加载到其中.

I have a webview and am trying to load simple UTF-8 text into it.

mWebView.loadData("將賦予他們的傳教工作標示為", "text/html", "UTF-8");

但 WebView 显示 ANSI/ASCII 垃圾.

But the WebView displays ANSI/ASCII garbage.

显然是编码问题,但我在告诉 webview 显示 Unicode 文本时遗漏了什么?

Obviously an encoding issue, but what am I missing in telling the webview to display the Unicode text?

这是一个 HelloWorld 应用程序.

This is a HelloWorld app.

推荐答案

使用:

mWebView.loadDataWithBaseURL(null, "將賦予他們的傳教工作標示為", "text/html", "utf-8", null);

或使用 WebSettings 和 setDefaultTextEncoding:

or using WebSettings with setDefaultTextEncoding:

WebSettings settings = mWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");

对于最新版本的 Android,API 16 到 22 已经过测试并且使用 loadData() 方法正常工作,需要 mimeType 包括:charset=utf-8".

WebView mWebView = (WebView) findViewById(R.id.myWebView);
WebSettings settings = mWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");                   
mWebView.loadData(myCharacters, "text/html; charset=utf-8",null);

  mWebView.loadData(myCharacters, "text/html; charset=utf-8","UTF-8");

这篇关于Android WebView UTF-8 未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆