不使用移动连接显示WebPage

时间:2012-04-22 19:53:57

标签: android webview webpage

我正在使用Android的WebView,因此我不必编程缩放和滚动。我在资源文件夹中有我想要的网页,这是我的代码。每次我在模拟器中运行时都会收到错误,没有这样的文件或目录。我的问题是我需要改变什么才能使其发挥作用?

刚刚更新了我的.java,现在我按 button1 时收到一个强制关闭,我的XML与下面相同。

好的,我修复了最后一部分,现在发生的一切是应用程序打开后我得到一个空白屏幕。

package com.DS;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

public class CheatMathActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){

            WebView webView = (WebView) findViewById(R.id.webView);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setSupportZoom(true);
            webView.loadUrl("file:///android_asset/stuffyoumustknowcoldforapcalc.htm");

    } });  
} 

}    

Ok这是我的XML,在添加WebView之前,我有一个家庭活动有两个按钮和一个textview,一旦我添加了WebView,图形布局显示的是一个灰色的屏幕,在中心显示WebView。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#4876ff" >
<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="90dp"
    android:textColor="#0000ff"
    android:text="AP Calculus" />
 <Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button1"
    android:layout_marginTop="38dp"
    android:text="More Coming Soon!" />
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="87dp"
    android:textColor="#FFFAFA"
    android:text="If you have any questions or want to report a bug, feel free to email                        me at fgoyer3856@gmail.com
                    Thank you!" />
<TextView
    android:id="@+id/titleView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp"
    android:gravity="center_vertical"
    android:text="Welcome to Math Cheat Sheet!"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<WebView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

好吧,如果您使用Intent(如上所述),则会显示新的外部浏览器实例并加载Uri中的Intent集,而不是使用您自己的WebView。 为了实现您的要求,您必须在活动布局中获得对WebView的引用,然后加载您的页面。此外,如果您要从assets/文件夹加载页面,则应使用AssetManagerWebView.loadData()方法。您的onCreate()方法应如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  // get a reference to the webview
  final WebView wV = (WebView) findViewById(R.id.webview);

  // get page content from assets/your_page.html as a String
  InputStream is = getAssets().open("your_page.html");
  BufferedReader br = new BufferedReader(new InputStreamReader(is));
  StringBuffer sb = new StringBuffer();
  String line;
  while((line=br.readLine())!=null){
    sb.append(line);
    sb.append("\n");
  }
  is.close();
  final String html = sb.toString();

  // get a reference to the button
  Button button1 = (Button) findViewById(R.id.button1);
  button1.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){

      // load html string into your WebView
      wV.loadData( html, "text/html", "utf-8" );
    }
  });

}

答案 1 :(得分:1)

在您的布局上放置一个webview。并在您的活动中加载您的html资产。

webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.loadUrl("file:///android_asset/stuffyoumustknowcoldforapcalc.htm");

不要忘记将文件stuffyoumustknowcoldforapcalc.htm放在名为assets的文件夹中。

package com.DS;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.webkit.WebView;

public class CheatMathActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    final Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){

                webView = (WebView) findViewById(R.id.webView1);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.getSettings().setBuiltInZoomControls(true);
                webView.getSettings().setSupportZoom(true);
                webView.loadUrl("file:///android_asset/stuffyoumustknowcoldforapcalc.htm");

        } });  
    } 


}