有人可以给出一个 android 中 webview 实现的确切例子吗

发布于 2024-12-22 18:12:42 字数 277 浏览 4 评论 0原文

您好,我正在使用 WebView 实现开发 Android 应用程序。

我遵循了官方android教程

在 Eclipse 中构建项目时,但在运行应用程序时,我没有收到错误:

应用程序意外停止工作

Hi I am developing android application using WebView implementation.

I followed the official android tutorial.

I am not getting errors when building my project in eclipse, but when running the application :

the application has stopped working unexpectedly

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

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

发布评论

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

评论(8

暮色兮凉城 2024-12-29 18:12:42

您的 Java 文件应如下所示:

public class WebViewSampleActivity extends Activity {
     WebView wb;
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        wb=(WebView)findViewById(R.id.webView1);        
        wb.getSettings().setJavaScriptEnabled(true);
        wb.getSettings().setLoadWithOverviewMode(true);
        wb.getSettings().setUseWideViewPort(true);
        wb.getSettings().setBuiltInZoomControls(true);
        wb.getSettings().setPluginState(WebSettings.PluginState.ON);
        wb.getSettings().setPluginsEnabled(true);           
        wb.setWebViewClient(new HelloWebViewClient());
        wb.loadUrl("http://www.examplefoo.com");        
    }
}

您的 xml 文件应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:layout_gravity="center">

    <WebView
        android:id="@+id/webView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" />

</LinearLayout>

在清单中的 之后使用以下内容:

 <uses-permission android:name="android.permission.INTERNET"/>

Your Java file should be like this:

public class WebViewSampleActivity extends Activity {
     WebView wb;
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        wb=(WebView)findViewById(R.id.webView1);        
        wb.getSettings().setJavaScriptEnabled(true);
        wb.getSettings().setLoadWithOverviewMode(true);
        wb.getSettings().setUseWideViewPort(true);
        wb.getSettings().setBuiltInZoomControls(true);
        wb.getSettings().setPluginState(WebSettings.PluginState.ON);
        wb.getSettings().setPluginsEnabled(true);           
        wb.setWebViewClient(new HelloWebViewClient());
        wb.loadUrl("http://www.examplefoo.com");        
    }
}

Your xml file should be like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:layout_gravity="center">

    <WebView
        android:id="@+id/webView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" />

</LinearLayout>

Use the following in your manifest after <uses-sdk><uses-sdk/>:

 <uses-permission android:name="android.permission.INTERNET"/>
|煩躁 2024-12-29 18:12:42

Android WebView loadurl 示例

Android WebView示例

WebView 用于在我们的应用程序中显示网页。让我们创建一个 WebView 并加载任何

中提供权限的网站

<uses-permission android:name="android.permission.INTERNET"/>

AndroidManifest.XML activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

MainActivity.Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity
{
private WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webview=(WebView)findViewById(R.id.webview);
//loads androidride homepage to webview
webview.loadUrl("https://www.androidride.com");
}
}

Android WebView loadurl example

Android WebView Example

WebView used to show webpages in our app. Let's make a WebView and load any website

provide permission in AndroidManifest.XML

<uses-permission android:name="android.permission.INTERNET"/>

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

MainActivity.Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity
{
private WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webview=(WebView)findViewById(R.id.webview);
//loads androidride homepage to webview
webview.loadUrl("https://www.androidride.com");
}
}
二智少女猫性小仙女 2024-12-29 18:12:42

开始吧:

    LinearLayout root = new LinearLayout(this);
root.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

WebView wv = new WebView(this);
wv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
wv.getSettings().setJavaScriptEnabled(true); 

wv.loadUrl("http://www.google.com");
root.addView(wv);

setContentView(root);

将此代码放入您的 onCreate-Method 中。
并且不要忘记在 Android Manifest 中设置 Permission Internet。

Here you go:

    LinearLayout root = new LinearLayout(this);
root.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

WebView wv = new WebView(this);
wv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
wv.getSettings().setJavaScriptEnabled(true); 

wv.loadUrl("http://www.google.com");
root.addView(wv);

setContentView(root);

Put this code in your onCreate-Method.
And don't forget to set the Permission Internet in Android Manifest.

小瓶盖 2024-12-29 18:12:42

WebView 是在应用程序内显示网页的视图。您还可以指定 HTML 字符串,并可以使用 WebView 在应用程序中显示它。

教程示例:http://www.viralandroid。 com/2015/09/android-webview-tutorial-with-examples.html

Activity_main.xml 文件

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

MainActivity.java 文件

import android.webkit.WebView;

...

webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.viralandroid.com");

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView.

Tutorial with Example: http://www.viralandroid.com/2015/09/android-webview-tutorial-with-examples.html

activity_main.xml file

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

MainActivity.java File

import android.webkit.WebView;

...

webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.viralandroid.com");
将军与妓 2024-12-29 18:12:42

xml文件

<WebView xmlns:android="http://schemas.android.com/apk/res/android"

   android:id="@+id/myweb"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"/>

java文件

 WebView web = (WebView) findViewById(R.id.myweb);

 web.setWebViewClient(new WebViewClient());

 web.getSettings().setLoadsImagesAutomatically(true);

 web.getSettings().setJavaScriptEnabled(true);

 web.getSettings().setBuiltInZoomControls(true);

 web.getSettings().setSupportZoom(true);

 web.getSettings().setLoadWithOverviewMode(true);

 web.getSettings().setUseWideViewPort(true);

 web.getSettings().setAllowContentAccess(true);

 web.loadUrl("https://www.technolifehacker.com");

 web.setWebViewClient(new WebViewClient(){

Xml file

<WebView xmlns:android="http://schemas.android.com/apk/res/android"

   android:id="@+id/myweb"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"/>

java file

 WebView web = (WebView) findViewById(R.id.myweb);

 web.setWebViewClient(new WebViewClient());

 web.getSettings().setLoadsImagesAutomatically(true);

 web.getSettings().setJavaScriptEnabled(true);

 web.getSettings().setBuiltInZoomControls(true);

 web.getSettings().setSupportZoom(true);

 web.getSettings().setLoadWithOverviewMode(true);

 web.getSettings().setUseWideViewPort(true);

 web.getSettings().setAllowContentAccess(true);

 web.loadUrl("https://www.technolifehacker.com");

 web.setWebViewClient(new WebViewClient(){
草莓酥 2024-12-29 18:12:42
  class WebViewClass extends WebViewClient {
        final  ProgressBar loadProgress;
        final  WebView wv;

        WebViewClass(WebView webView, ProgressBar progressBar) {
            this.wv = webView;
            this.loadProgress = progressBar;
        }

        public void onPageFinished(WebView view, String url) {
            wv.setVisibility(View.VISIBLE);
            loadProgress.setVisibility(View.INVISIBLE);
            view.clearCache(true);
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            wv.loadData("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + "<center>" + getString(R.string.erroopsproblem) + ".</center>", "text/html", "UTF-8");
        }
    }

现在在 onCreate() 上使用此类

WebView wv = (WebView) findViewById(R.id.webview);
        wv.setWebViewClient(new WebViewClient());
        wv.getSettings().setLoadsImagesAutomatically(true);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setScrollBarStyle(View.VISIBLE);
        wv.getSettings().setBuiltInZoomControls(true);
        wv.getSettings().setSupportZoom(true);
        wv.getSettings().setLoadWithOverviewMode(true);
        wv.getSettings().setUseWideViewPort(true);
        wv.getSettings().setAllowContentAccess(true);
        wv.loadUrl(Dest);
        wv.setVisibility(View.INVISIBLE);
        wv.setWebViewClient(new WebViewClass(wv, loadProgress));
  class WebViewClass extends WebViewClient {
        final  ProgressBar loadProgress;
        final  WebView wv;

        WebViewClass(WebView webView, ProgressBar progressBar) {
            this.wv = webView;
            this.loadProgress = progressBar;
        }

        public void onPageFinished(WebView view, String url) {
            wv.setVisibility(View.VISIBLE);
            loadProgress.setVisibility(View.INVISIBLE);
            view.clearCache(true);
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            wv.loadData("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + "<center>" + getString(R.string.erroopsproblem) + ".</center>", "text/html", "UTF-8");
        }
    }

Now use this class on onCreate() like

WebView wv = (WebView) findViewById(R.id.webview);
        wv.setWebViewClient(new WebViewClient());
        wv.getSettings().setLoadsImagesAutomatically(true);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setScrollBarStyle(View.VISIBLE);
        wv.getSettings().setBuiltInZoomControls(true);
        wv.getSettings().setSupportZoom(true);
        wv.getSettings().setLoadWithOverviewMode(true);
        wv.getSettings().setUseWideViewPort(true);
        wv.getSettings().setAllowContentAccess(true);
        wv.loadUrl(Dest);
        wv.setVisibility(View.INVISIBLE);
        wv.setWebViewClient(new WebViewClass(wv, loadProgress));
梦里南柯 2024-12-29 18:12:42

除了@ghost回答之外,如果您希望您的网络视图像浏览器一样功能齐全,即您可以前进和后退,那么您需要覆盖您的活动的 onBackPressed 方法,如下所示

@Override
public void onBackPressed() {
if (webView.canGoBack()) {
    webView.goBack();
  } else {
    super.onBackPressed();
  }
}

In addition to @ghost answer, if you want your web-view to be fully functional like a browser i.e. you can go forward and back then you need to override onBackPressed method of your activity like below

@Override
public void onBackPressed() {
if (webView.canGoBack()) {
    webView.goBack();
  } else {
    super.onBackPressed();
  }
}
琉璃梦幻 2024-12-29 18:12:42

您必须

  1. 创建一个 Layout XML 文件,例如 'test.xml'
  2. 创建一个继承 Activity 的类,例如 'wvtutorial
  3. 在其生命周期的 OnCreate 方法中,创建一个 WebView 实例,例如 'wview' ,通过初始化值 null

You must

  1. Create a Layout XML file for example 'test.xml'
  2. Create a class that inherited Activity for example 'wvtutorial
  3. In OnCreate method of its life cycle, create an instance of WebView , for example 'wview' , by initialized value null
  4. And other
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文