【发布时间】:2013-07-12 13:58:06
【问题描述】:
我正在尝试从 html 页面实现 C 程序的语法高亮显示。
"index.html","prettify.css","prettify.js" 存放在 assets 文件夹中
这是我的 webview 的代码:
package com.example.javascriptexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv=(WebView) findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setDomStorageEnabled(true);
wv.loadUrl("file:///android_asset/index.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
对应的布局文件(XML)是:
<RelativeLayout 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"
tools:context=".MainActivity" >
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/webView" />
</RelativeLayout>
index.html如下:
<HTML>
<body>
<pre>
<code class="prettyprint">
/*---------- PROGRAM USING IF STATEMENT --------*/
#include < stdio.h >
#include < conio.h >
void main()
{
int count, i;
float weight, height;
count = 0;
printf("Enter weight and height for 10 boys\n");
for (i =1; i <= 10; i++)
{
scanf("%f %f", &weight, &height);
if (weight < 50 && height > 170)
count = count + 1;
}
printf("Number of boys with weight < 50 kg\n");
printf("and height > 170 cm = %d\n", count);
getch();
}
</code>
</pre>
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
</body>
</HTML>
我尝试在互联网上搜索,但无法解决此问题。 文本正在显示,但没有语法突出显示(即,我在 webview 中实现 javascript 的方式可能有问题。 然而,该脚本在我的计算机上运行顺利,所以也许我犯了一些错误,因为我是 javascript 和 android 的初学者。 顺便说一句,美化的css和js文件不是我写的,我是从网上下载的(code.google.com)
我遇到了其他类似的问题。提供的解决方案之一是“您必须强制应用程序等待(尝试一下,在您的 web 视图中放置一个断点,然后让它继续运行,您会看到这次 web 视图将可靠地加载 javascript。而如果您只是让应用程序在没有断点的情况下加载,它经常“跳过”javascript)。需要某种强制线程等待方法。”
-如何实现这个?即,如何放置断点然后让它继续?
【问题讨论】:
-
您的清单中有
INTERNET权限吗? LogCat 是否显示任何警告或注意错误? -
我已包含互联网权限,logcat 不显示任何警告或错误
标签: javascript android css webview prettify