Hướng dẫn load html in webview android - tải html trong webview android

Tôi có một chuỗi HTML chứa điều này:

    
    
      
      
        
      
      
        

        
      
    

Tôi cần hiển thị trang web đó vào một WebView trong Android.

Tôi đã thử với tất cả những điều này:

webView.loadDataWithBaseURL(null, txt, "text/html", "UTF-8", null);
webView.loadDataWithBaseURL("x-data://base", txt, "text/html", "UTF-8", null);      
webView.loadDataWithBaseURL("notreal/", txt, "text/htm", "utf-8",null);

Ngoài ra, tôi đã thử xóa thẻ doctype:

txt=txt.replace("", "");

Không ai trong số họ có công việc. Tôi chỉ đạt được để hiển thị chuỗi vào WebView (mã HTML), nhưng không phải là trang web phải được tạo bằng mã HTML đó.

Chuyện gì thế?


Ví dụ này chứng minh về cách tải nội dung HTML trong Android WebView.

Bước 1 - Tạo một dự án mới trong Android Studio, đi đến 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. − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Bước 2 - Thêm mã sau vào Res/Layout/Activity_Main.xml. − Add the following code to res/layout/activity_main.xml.


   

Trong mã trên, chúng tôi đã xem Web View để hiển thị nội dung HTML.

Bước 3 - Thêm mã sau vào SRC/MainActivity.java − Add the following code to src/MainActivity.java

package com.example.myapplication;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
   @RequiresApi(api = Build.VERSION_CODES.P)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final ProgressDialog progressDialog = new ProgressDialog(this);
      progressDialog.setMessage("Loading Data...");
      progressDialog.setCancelable(false);
      WebView web_view = findViewById(R.id.web_view);
      web_view.requestFocus();
      web_view.getSettings().setLightTouchEnabled(true);
      web_view.getSettings().setJavaScriptEnabled(true);
      web_view.getSettings().setGeolocationEnabled(true);
      web_view.setSoundEffectsEnabled(true);
      web_view.loadData("Hello, world!",
      "text/html", "UTF-8");
      web_view.setWebChromeClient(new WebChromeClient() {
         public void onProgressChanged(WebView view, int progress) {
            if (progress < 100) {
               progressDialog.show();
            }
            if (progress = = 100) {
               progressDialog.dismiss();
            }
         }
      });
   }
}

Hãy cố gắng chạy ứng dụng của bạn. Tôi cho rằng bạn đã kết nối thiết bị di động Android thực tế của bạn với máy tính của bạn. Để 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 Run & nbsp; từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn và sau đó kiểm tra thiết bị di động của bạn sẽ hiển thị màn hình mặc định của bạn -

Hướng dẫn load html in webview android - tải html trong webview android
 icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –

Hướng dẫn load html in webview android - tải html trong webview android

Nhấn vào đây để tải xuống mã dự án

Hướng dẫn load html in webview android - tải html trong webview android

Cập nhật vào ngày 30 tháng 3 năm 2019 22:30:25

  • Câu hỏi và câu trả lời liên quan
  • Làm thế nào để tải WebView từ bộ đệm trong Android?
  • Làm thế nào để kích hoạt nội dung hỗn hợp WebView trong Android?
  • Làm thế nào để tải các tệp từ thư mục tài sản vào WebView trong Android?
  • Làm thế nào để phát hiện nhấp vào nút HTML thông qua JavaScript trong Android Webview?
  • Android 4.0.1 Breaks WebView HTML 5 Lưu trữ cục bộ?
  • Làm thế nào để thêm WebView Focus trong Android?
  • Làm thế nào để xóa lịch sử WebView trong Android?
  • Làm thế nào để lưu mật khẩu trong Android Webview?
  • Làm thế nào để hiển thị PDF trong Android Webview?
  • Làm thế nào để kích hoạt tập lệnh Java WebView trong Android?
  • Làm thế nào để kích hoạt duyệt web an toàn trong Android?
  • Làm thế nào để kích hoạt các điều khiển Zoom của WebView trong Android?
  • Làm thế nào để đặt màn hình Fit WebView trong Android?
  • Làm thế nào để bật nút quay lại trong Android Webview?
  • Làm thế nào để tạo một WebView trong ứng dụng Android?

Làm cách nào để tải mã HTML vào WebView?

Bước 1 - Tạo một dự án mới trong Android Studio, đi đến 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. Trong mã trên, chúng tôi đã xem Web View để hiển thị nội dung HTML. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

Làm cách nào để kích hoạt JavaScript trên Android Webview?

Bật JavaScript JavaScript bị vô hiệu hóa trong WebView theo mặc định.Bạn có thể kích hoạt nó thông qua các trang web được đính kèm với WebView của bạn.Bạn có thể truy xuất các cài đặt web với getSinstall (), sau đó bật javaScript bằng setJavascriptenable ().WebView myWebView = (WebView) FindViewById (r.through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

Tôi nên đặt tệp HTML ở đâu trong Android Studio?

Bước 1: Để thêm tệp HTML cục bộ vào dự án Android của bạn, phải có một thư mục tài sản trong đó.Để tạo một thư mục tài sản trong Android Studio, hãy mở dự án của bạn ở chế độ Android trước tiên như trong hình ảnh dưới đây.Bước 2: Chuyển đến Ứng dụng> Nhấp chuột phải> mới> Thư mục> Thư mục tài sản và tạo thư mục tài sản.Go to the app > right-click > New > Folder > Asset Folder and create the asset folder.

Làm thế nào nhập tệp HTML vào React Native?

Tải tệp HTML cục bộ hoặc URL trong React Native bằng cách sử dụng React-I-WebView..
2.1 1. Tải trang web từ URL ..
2.2 2. Tải trang HTML từ một biến với chuỗi HTML ..
2.3 3. Tải tệp HTML cục bộ ..