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

วิธีทำให้ส่วนหนึ่งของ TextView คลิกได้ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีทำให้ข้อความบางส่วนใน TextView เป็นลิงก์ที่คลิกได้ โดยใช้ SpannableString ร่วมกับ ClickableSpan เหมาะสำหรับการสร้างเงื่อนไขการใช้งาน นโยบายความเป็นส่วนตัว หรือ Hashtag ที่กดได้

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

เปิด Android Studio → File → New Project → Empty Activity

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ (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/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I want THIS and THIS to be CLICKED!"
        android:layout_centerInParent="true"
        android:textSize="16sp"
        android:textStyle="bold|italic"/>

</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView textView;

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

        textView = findViewById(R.id.textView);
        String text = "I want THIS and THIS to be CLICKED";

        SpannableString spannableString = new SpannableString(text);

        // สร้าง ClickableSpan สำหรับคำว่า "THIS" ตัวแรก (ตำแหน่ง 7-11)
        ClickableSpan clickableSpan1 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "THIS (1st)", Toast.LENGTH_SHORT).show();
            }
        };

        // สำหรับคำว่า "THIS" ตัวที่สอง (ตำแหน่ง 16-20)
        ClickableSpan clickableSpan2 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "THIS (2nd)", Toast.LENGTH_SHORT).show();
            }
        };

        // สำหรับคำว่า "CLICKED" (ตำแหน่ง 27-34)
        ClickableSpan clickableSpan3 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "CLICKED", Toast.LENGTH_SHORT).show();
            }
        };

        // กำหนด Span ให้กับช่วงตำแหน่งที่ต้องการ
        spannableString.setSpan(clickableSpan1, 7, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(clickableSpan2, 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(clickableSpan3, 27, 34, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        // นำ SpannableString ไปเซ็ตให้ TextView
        textView.setText(spannableString);
        
        // สำคัญ: ต้องตั้ง MovementMethod เพื่อให้คลิกได้
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

ขั้นตอนที่ 4: 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>

การทดสอบ

รันแอป → กดที่คำว่า "THIS" และ "CLICKED" จะเห็น Toast แสดงข้อความตอบสนอง

วิธีทำให้ส่วนหนึ่งของ TextView คลิกได้ใน Android วิธีทำให้ส่วนหนึ่งของ TextView คลิกได้ใน Android วิธีทำให้ส่วนหนึ่งของ TextView คลิกได้ใน Android

เคล็ดลับเพิ่มเติม

  • ใช้ ForegroundColorSpan เปลี่ยนสีข้อความให้ดูเป็นลิงก์ (เช่น สีน้ำเงิน)
  • ใช้ UnderlineSpan เพิ่มเส้นใต้
  • สำหรับ URL จริง ใช้ URLSpan แทน ClickableSpan
  • คำนวณ Index อัตโนมัติด้วย text.indexOf("คำที่ต้องการ") แทนการนับมือ