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

Làm cách nào để hiển thị HTML trong TextView trong Android?


Trong một số trường hợp, chúng ta cần hiển thị HTML dưới dạng văn bản trong android. Đây là giải pháp đơn giản để hiển thị HTML trong TextView 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"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:tools = "https://schemas.android.com/tools"
   android:id = "@+id/rootview"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:orientation = "vertical"
   tools:context = ".MainActivity">
   <TextView
      android:id = "@+id/htmlToTextView"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />
</LinearLayout>

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

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v4.text.HtmlCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   String htmlText = "<h2>What is Android?</h2>\n" + "<p>Android is an open source and Linux-based <b>Operating System</b> for mobile devices such as smartphones and tablet computers.
Android was developed by the <i>Open Handset Alliance</i>, led by Google, and other companies.</p>\n" + "<p>Android offers a unified approach to application development for mobile devices which means developers need only develop for Android, and their applications should be able to run on different devices powered by Android.</p>\n" + "<p>The first beta version of the Android Software Development Kit (SDK) was released by Google in 2007 whereas the first commercial version, Android 1.0, was released in September 2008.</p>";
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      TextView htmlToTextView = findViewById(R.id.htmlToTextView);
      htmlToTextView.setText(HtmlCompat.fromHtml(htmlText, 0));
   }
}

Trong ví dụ trên, chúng tôi giữ các thẻ HTML trong một chuỗi dưới dạng htmlText và thêm chuỗi vào textview như được hiển thị bên dưới.

htmlToTextView.setText(HtmlCompat.fromHtml(htmlText, 0));

Trong đoạn mã trên, chúng tôi đang lấy dữ liệu HTML từ fromHtml () và thêm vào textview bằng cách sử dụng setText (). 0 là cờ. bạn có thể gán cờ theo tài nguyên dự án.

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 -

Làm cách nào để hiển thị HTML trong TextView trong Android?

Trong ví dụ trên, nó hiển thị các thẻ HTML một chuỗi.