Computer >> Máy Tính >  >> Lập trình >> Android

Ai đó có thể đưa ra một ví dụ chính xác về triển khai webview trong android không

Trước khi bắt đầu triển khai chế độ xem web, chúng ta nên biết chế độ xem web là gì. Chế độ xem web là phần mở rộng của chế độ xem và nó được sử dụng để hiển thị nội dung HTML hoặc các trang web.

Các phương pháp có sẵn trong webview.

  • clearHistory () - nó được sử dụng để xóa lịch sử xem web

  • tiêu diệt () - Nó được sử dụng để phá hủy trạng thái nội bộ của webview.

  • getUrl () −it được sử dụng để trả về url chế độ xem web hiện tại.

  • getTitle () - Nó được sử dụng để trả về tiêu đề chế độ xem web hiện tại.

  • canGoBack () - Nó cho biết về chế độ xem web hiện tại có các mục lịch sử quay lại.

Sử dụng webview, nó sẽ mở nội dung webview trong các trình duyệt Android mặc định. Nếu bạn muốn mở bên trong ứng dụng. ShouldOverrideUrlLoading như hình bên dưới.

private class MyWebViewClient extends WebViewClient {
   @Override
   public boolean shouldOverrideUrlLoading(WebView webView, String url) {
      return false;
   }
}

Ví dụ này minh họa về cách triển khai webview trong android.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout
   xmlns:android = "https://schemas.android.com/apk/res/android" xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent">
   <WebView
      android:layout_width = "match_parent"
      android:layout_height = "match_parent"
      android:id = "@+id/webView" />
</android.support.constraint.ConstraintLayout>

Bước 3 - Thêm mã sau vào src / MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import timber.log.Timber;
public class MainActivity extends AppCompatActivity {
   private WebView simpleWebView;
   private ProgressBar loadProgress;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      simpleWebView=findViewById(R.id.webView);
      simpleWebView.setWebViewClient(new WebViewClient());
      simpleWebView.getSettings().setLoadsImagesAutomatically(true);
      simpleWebView.getSettings().setJavaScriptEnabled(true);
      simpleWebView.setScrollBarStyle(View.VISIBLE);
      simpleWebView.getSettings().setBuiltInZoomControls(true);
      simpleWebView.getSettings().setSupportZoom(true);
      simpleWebView.getSettings().setLoadWithOverviewMode(true);
      simpleWebView.getSettings().setUseWideViewPort(true);
      simpleWebView.getSettings().setAllowContentAccess(true);
      simpleWebView.loadUrl("https://www.tutorialspoint.com/");
   }
   @Override
   public void onBackPressed() {
      if (simpleWebView.canGoBack()) {
         simpleWebView.goBack();
      } else {
         super.onBackPressed();
      }
   }
}

Trong đoạn mã trên, bạn có thể cung cấp trang web của riêng mình trong loadUrl ();

Bước 4 - Thêm mã sau vào AndroidManifest.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "https://schemas.android.com/apk/res/android"
   package = "com.example.andy.myapplication">
<uses-permission android:name = "android.permission.INTERNET"/>
   <application
      android:allowBackup = "true"
      android:icon = "@mipmap/ic_launcher"
      android:label = "@string/app_name"
      android:roundIcon = "@mipmap/ic_launcher_round"
      android:supportsRtl = "true"
      android:theme = "@style/AppTheme">
      <activity android:name = ".MainActivity">
         <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Trong đoạn mã trên, chúng tôi đã cấp quyền truy cập Internet vì chúng tôi đang gọi trang web từ nguồn internet.

Bước 5 - Thêm mã sau vào res / values ​​/ string.xml.

<resources>
   <string name = "app_name">My Application</string>
   <string name = "erroopsproblem">Something error</string>
</resources>

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn.

Ai đó có thể đưa ra một ví dụ chính xác về triển khai webview trong android không

Bây giờ khi bạn nhấp vào một cái gì đó. Ví dụ nhấp vào biểu tượng HTML như hình trên. nó sẽ cho kết quả như hình dưới đây

Ai đó có thể đưa ra một ví dụ chính xác về triển khai webview trong android không