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

Tạo liên kết có thể nhấn (clickable links) trong TextView Android Kotlin

Giới thiệu

Ví dụ này minh họa cách làm cho một phần văn bản trong TextView trở thành liên kết có thể nhấn (hyperlink) để mở trình duyệt, sử dụng HTML trong strings.xml và LinkMovementMethod.

Các bước thực hiện

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

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

Bước 2: Giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Tutorials Point"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="32sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textViewLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="@string/messageWithLink"
        android:textSize="24sp"
        android:textStyle="bold" />

</RelativeLayout>

Bước 3: Chuỗi chứa link (res/values/strings.xml)

<resources>
    <string name="app_name">Link Demo</string>
    <string name="messageWithLink"><a href="https://www.google.com/">Tap here to open Google</a></string>
</resources>

Bước 4: MainActivity.kt

import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        val textView = findViewById<TextView>(R.id.textViewLink)
        textView.movementMethod = LinkMovementMethod.getInstance()
    }
}

Bước 5: AndroidManifest.xml

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

    <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ị, nhấn Run. Văn bản "Tap here to open Google" sẽ có gạch chân và màu xanh, nhấn sẽ mở trình duyệt.

Mẹo nâng cao: Dùng SpannableString + ClickableSpan để xử lý click tùy chỉnh (không mở browser mà làm action khác).