Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Linkify TextView trong Android: Tự động tạo link web, email, số điện thoại

Giới thiệu về Linkify

Linkify trong Android tương tự hyperlink trong HTML. Nó tự động phát hiện và chuyển đổi các mẫu văn bản (URL, email, số điện thoại, địa chỉ) thành liên kết có thể nhấn. Khi người dùng nhấn, hệ thống mở ứng dụng mặc định tương ứng (trình duyệt, email client, dialer, bản đồ).

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project, điền thông tin cần thiết.

Bước 2: Thiết kế giao diện (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:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nội dung sẽ được linkify"
        android:textSize="20sp"
        android:padding="16dp"
        android:autoLink="web|email|phone|map"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Lưu ý: Thuộc tính android:autoLink trong XML cũng có thể linkify tự động mà không cần code Java.

Bước 3: Viết logic (src/MainActivity.java)

package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        TextView textView = findViewById(R.id.result);

        String content = "Chào mừng đến với TutorialsPoint.com! " +
                "Liên hệ: support@tutorialspoint.com | " +
                "Hotline: 0909-123-456 | " +
                "Địa chỉ: 123 Nguyễn Văn Linh, Quận 7, TP.HCM";

        textView.setText(content);

        // Linkify thủ công bằng code - linh hoạt hơn autoLink XML
        Linkify.addLinks(textView, Linkify.ALL);
        // Hoặc chọn lọc: Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS | Linkify.MAP_ADDRESSES
    }
}

Các loại Linkify Mask

MaskMô tảHành động khi nhấn
Linkify.WEB_URLSPhát hiện URL web (https://, https://, www.)Mở trình duyệt mặc định
Linkify.EMAIL_ADDRESSESPhát hiện email (user@domain.com)Mở ứng dụng email mặc định
Linkify.PHONE_NUMBERSPhát hiện số điện thoạiMở dialer (keo số)
Linkify.MAP_ADDRESSESPhát hiện địa chỉ địa lýMở Google Maps
Linkify.ALLKết hợp tất cả các loại trênTùy loại liên kết

Bước 4: Cấu hình quyền (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>

Chạy ứng dụng

Kết nối thiết bị và chạy ứng dụng.

Linkify TextView trong Android: Tự động tạo link web, email, số điện thoại

Nhấn vào link web (TutorialsPoint.com):

Linkify TextView trong Android: Tự động tạo link web, email, số điện thoại

Mở website trong trình duyệt mặc định:

Linkify TextView trong Android: Tự động tạo link web, email, số điện thoại

Mẹo nâng cao:
  • Tùy chỉnh regex riêng: Linkify.addLinks(textView, pattern, "myapp://")
  • Tùy chỉnh TransformFilter để chỉnh sửa URL trước khi mở
  • Đặt android:linksClickable="true" và android:autoLink="..." trong XML cho cách nhanh nhất
  • Cần quyền INTERNET cho link web, CALL_PHONE cho số điện thoại (nếu tự xử lý intent)