Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

Linkify TextView ใน Android คืออะไร และการใช้งาน

บทนำ

Linkify เป็นคลาสใน Android ที่แปลงข้อความใน TextView ให้กลายเป็น Hyperlink อัตโนมัติ คล้ายกับ <a> tag ใน HTML รองรับ URL, อีเมล, หมายเลขโทรศัพท์ และที่อยู่บนแผนที่

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio เลือก File → New Project กรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: ออกแบบ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="Result Data"
        android:textSize="20sp"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ขั้นตอนที่ 3: เขียนโค้ด Java (src/MainActivity.java)

package com.example.myapplication;

import android.os.Bundle;
import androidx.appcompat.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);

        // ข้อความที่มี URL, อีเมล, โทรศัพท์
        String text = "Visit TutorialsPoint.com for tutorials. " +
                "Contact us at support@tutorialspoint.com or call +1-800-555-0199. " +
                "Location: 1600 Amphitheatre Parkway, Mountain View, CA.";

        textView.setText(text);

        // ใช้ Linkify.ALL เพื่อจับทุกประเภท (WEB_URLS, EMAIL_ADDRESSES, PHONE_NUMBERS, MAP_ADDRESSES)
        Linkify.addLinks(textView, Linkify.ALL);

        // หรือระบุเฉพาะประเภทที่ต้องการ:
        // Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES);
    }
}

ขั้นตอนที่ 4: เพิ่ม Permission ใน AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.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>

การทดสอบแอปพลิเคชัน

รันแอปบนอุปกรณ์จริง

Linkify TextView ใน Android คืออะไร และการใช้งาน

ข้อความที่เป็น Link จะมีสีและขีดเส้นใต้ คลิกได้

Linkify TextView ใน Android คืออะไร และการใช้งาน

คลิก URL จะเปิด Browser, คลิก Email จะเปิด Email Client, คลิก Phone จะเปิด Dialer

Linkify TextView ใน Android คืออะไร และการใช้งาน

Linkify Mask Types

Mask คำอธิบาย ตัวอย่าง
Linkify.WEB_URLS เว็บไซต์ (http, https, www.) https://example.com, www.google.com
Linkify.EMAIL_ADDRESSES อีเมล user@example.com
Linkify.PHONE_NUMBERS หมายเลขโทรศัพท์ +1-800-555-0199, 02-123-4567
Linkify.MAP_ADDRESSES ที่อยู่ (เปิด Google Maps) 1600 Amphitheatre Parkway, Mountain View, CA
Linkify.ALL รวมทุกประเภทข้างต้น -

การกำหนด Pattern เอง (Custom Pattern)

// ตัวอย่าง: ทำให้คำว่า "Ticket #12345" กลายเป็น Link
Pattern pattern = Pattern.compile("Ticket #\\d+");
String scheme = "myapp://ticket/";
Linkify.addLinks(textView, pattern, scheme);

สรุป

ใช้ Linkify.addLinks(TextView, mask) เพื่อทำให้ TextView ตรวจจับและแปลงข้อความเป็น Link ได้อัตโนมัติ รองรับหลายประเภท ลดความซับซ้อนของการจัดการ ClickableSpan เอง