我认为一段语法可以浏览本地网页。但我想隐藏地址。我测试了很多次都不成功。
package org.webViewEx;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewEx extends Activity {
// Controls
private WebView m_webView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Initialize controls
m_webView = (WebView)findViewById(R.id.wvBrowser);
// Initialize web view
WebSettings webViewSettings = m_webView.getSettings();
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setDomStorageEnabled(true);
webViewSettings.setDatabaseEnabled(true);
webViewSettings.setDatabasePath("/data/data/"+getPackageName()+"/databases");
// Override functions so that javascript alert() and confirm() can work
m_webView.setWebChromeClient(new WebChromeClient()
{
@Override
public boolean onJsAlert(WebView view, String url, String message,
final JsResult result)
{
new AlertDialog.Builder(WebViewEx.this)
.setMessage(message)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}})
.show();
return true;
}
@Override
public boolean onJsConfirm(WebView view, String url,
String message, final JsResult result)
{
new AlertDialog.Builder(WebViewEx.this)
.setMessage(message)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}})
.show();
return true;
}
});
// Initialize a webview client
m_webView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl("file:///android_asset/ts.htm");
return true;
}
});
// Show main page
m_webView.loadUrl("file:///android_asset/anttree.htm");
}
}
我从网上了解到。使用这种方法可以成功隐藏。
m_webView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl("file:///android_asset/ts.htm");
return true;
}
});
但是没用 告诉我怎么做 thx