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

Tạo liên kết có thể nhấn trong TextView Android

Tạo liên kết có thể nhấn trong TextView Android

Hướng dẫn này minh họa cách tạo hyperlink có thể nhấn trong TextView để mở trình duyệt web.

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

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

Bước 2: Thiết kế giao diện (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:id="@+id/textViewLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:textStyle="bold"
        android:gravity="center"
        android:text="@string/messageWithLink" />
</RelativeLayout>

Bước 3: Cấu hình strings.xml

Mở res/values/strings.xml và thêm link HTML:

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

Bước 4: Code MainActivity.java

package app.com.sample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textView = findViewById(R.id.textViewLink);
        // Quan trọng: Bật LinkMovementMethod để link có thể nhấn
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

Bước 5: Cấu hình AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
    <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ị, chạy app. TextView sẽ hiển thị "Tap here to open Link" với kiểu chữ liên kết (gạch chân, màu xanh). Khi nhấn, trình duyệt sẽ mở google.com.

Lưu ý: Nếu thiếu setMovementMethod(LinkMovementMethod.getInstance()), link sẽ hiển thị nhưng không thể nhấn. Đây là lỗi thường gặp.

Mở rộng: Tạo link động từ code Java

// Tạo SpannableString với link động
SpannableString spannable = new SpannableString("Xem thêm tại Google");
ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com")));
    }
};
spannable.setSpan(clickableSpan, 10, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
textView.setMovementMethod(LinkMovementMethod.getInstance());