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 -
Nhấn vào đây để tải xuống mã dự án
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?