Android WebView中的汉字

发布于 2024-11-10 15:49:29 字数 2121 浏览 4 评论 0原文

我有一个 html 代码,保存在 home.txt 文件中并将其放置在 raw 文件夹中。现在我想在 WebView 中显示相同的内容。我使用了以下代码。

homeWebview = (WebView) findViewById(R.id.homeWebview);
InputStream fileStream = getResources().openRawResource(R.raw.home); 
int fileLen = fileStream.available();

// Read the entire resource into a local byte buffer. 
byte[] fileBuffer = new byte[fileLen]; 
fileStream.read(fileBuffer);

fileStream.close();  
displayText = new String(fileBuffer);

//Display content.          
homeWebview.loadData(displayText, "text/html", "utf-8");

它运行良好。现在我必须在html中显示一些汉字。我在home.txt中添加了汉字。这是html代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Language</title>
<style type="text/css">
body {font-family: HelveticaExt-normal, Helvetica, Arial, sans-serif;margin:0;font-size:20px;color:#000000;}
.mainWrapper {margin:0;}
p {margin: 10px;}
p.bodytext {margin: 0 10px 20px;}
.headerText { background:#eeeeee; background: -webkit-gradient(linear, left top, left bottom, from(#fefefe), to(#e0e0e0));border-top: 2px solid #000000;padding-bottom:7px;}
.headerText p {margin: 10px 10px 0 10px;}
a {color:#324f85;text-decoration:underline;}
.headerbar {border-top:2px solid #000000;border-bottom:2px solid #000000;height:34px;font-family:HelveticaExt-bold, Helvetica, Sans-serif;color:#fff;font-size:18px;padding-left:10px;line-height:34px;margin:0;
 background:-webkit-gradient(linear, left top, left bottom, from(#494e4f), to(#787a7c));
}
.navigationList {list-style-type:none;padding:0;margin:0;}
.navigationList li {height:60px;border-bottom:2px solid #e0e0e0;background-position:10px center;background-repeat:no-repeat;padding-left:60px;line-height:28px;
}
</style>
</head>

<body>
<div class="mainWrapper">
    <div class="headerText">
        <p欢迎您到泰国语言主持人!<a href="http://www.test.com">www.test.com</a> 欲了解更多信息!</p>
    </div>
</div>
</body>

现在可以很好地显示常用字符,但所有中文字符都显示为未知字符。我的代码有什么问题?

I have a html code which I saved in home.txt file and placed it raw folder. Now I want to display the same in a WebView. I used the following code for it.

homeWebview = (WebView) findViewById(R.id.homeWebview);
InputStream fileStream = getResources().openRawResource(R.raw.home); 
int fileLen = fileStream.available();

// Read the entire resource into a local byte buffer. 
byte[] fileBuffer = new byte[fileLen]; 
fileStream.read(fileBuffer);

fileStream.close();  
displayText = new String(fileBuffer);

//Display content.          
homeWebview.loadData(displayText, "text/html", "utf-8");

It is working fine. Now I have to display some Chinese characters in the html. I added the Chinese character in home.txt. Here it is the html code:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Language</title>
<style type="text/css">
body {font-family: HelveticaExt-normal, Helvetica, Arial, sans-serif;margin:0;font-size:20px;color:#000000;}
.mainWrapper {margin:0;}
p {margin: 10px;}
p.bodytext {margin: 0 10px 20px;}
.headerText { background:#eeeeee; background: -webkit-gradient(linear, left top, left bottom, from(#fefefe), to(#e0e0e0));border-top: 2px solid #000000;padding-bottom:7px;}
.headerText p {margin: 10px 10px 0 10px;}
a {color:#324f85;text-decoration:underline;}
.headerbar {border-top:2px solid #000000;border-bottom:2px solid #000000;height:34px;font-family:HelveticaExt-bold, Helvetica, Sans-serif;color:#fff;font-size:18px;padding-left:10px;line-height:34px;margin:0;
 background:-webkit-gradient(linear, left top, left bottom, from(#494e4f), to(#787a7c));
}
.navigationList {list-style-type:none;padding:0;margin:0;}
.navigationList li {height:60px;border-bottom:2px solid #e0e0e0;background-position:10px center;background-repeat:no-repeat;padding-left:60px;line-height:28px;
}
</style>
</head>

<body>
<div class="mainWrapper">
    <div class="headerText">
        <p欢迎您到泰国语言主持人!<a href="http://www.test.com">www.test.com</a> 欲了解更多信息!</p>
    </div>
</div>
</body>

Now it is displaying the common characters well, but all the Chinese char is showing as unknown char. What is the prob in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚风撩人 2024-11-17 15:49:29

使用 webView.loadDataWithBaseURL (null, text, "text/html", "utf-8", null);

use webView.loadDataWithBaseURL (null, text, "text/html", "utf-8", null);

花落人断肠 2024-11-17 15:49:29

home.txt 文件不是 UTF-8 编码的,我在记事本++中打开它并更改编码。现在可以正常显示了。

The home.txt file was not in UTF-8 encoded, I open it in notepad++ and change the encoding. Now it is showing properly.

铁憨憨 2024-11-17 15:49:29

我认为这一行有问题:

displayText = new String(fileBuffer);

你应该改为

displayText = new String(fileBuffer, "UTF-8");

I think problem in this line:

displayText = new String(fileBuffer);

You should change to

displayText = new String(fileBuffer, "UTF-8");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文