Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

讓 TextView 也能觸發 Intent:跳轉到自己的 Activity

先前那篇做法可以替 TextView 加上事件響應,但只支援 web、email、phone、map 這幾種內建類型。如果想在點擊文字之後跳轉到自己的 Activity,就得自己動手做一個 Span。答案就在下面的程式碼裡。

Activity 端:綁定 Intent

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    linkify = new LinkifyUtil(this);
    Intent intent = new Intent("com.example.Android.note.ADD");
    intent.setData(Uri.parse("content://com.example.android.note/item"));

    linkify.addIntentLink(intent, (TextView) findViewById(R.id.tv_new_item));
}

LinkifyUtil:把 Intent 包成可點擊的 span

這個工具類別接收一個 Intent 與目標 TextView,用 SpannableString 包住原始文字,再套用自製的 IntentSpan,最後掛上 LinkMovementMethod 讓它具備連結行為:

package com.example.android.note.util;

import android.app.Activity;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * @author eidolon
 */
public class LinkifyUtil {

    private final Activity currentActivity;

    public LinkifyUtil(Activity activity) {
        this.currentActivity = activity;
    }

    public void addIntentLink(final Intent intent, final TextView view,
                              final int start, final int end) {
        CharSequence source = view.getText();

        if (source instanceof Spanned) {
            IntentSpan[] spans = ((Spanned) source).getSpans(start, end, IntentSpan.class);
            if (spans.length > 0) {
                return;   // 已有重疊的 span,直接跳過避免衝突
            }
        }

        SpannableString spannableString = new SpannableString(source);
        spannableString.setSpan(new IntentSpan(new OnClickListener() {

            public void onClick(View view) {
                currentActivity.startActivity(intent);
            }
        }), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        view.setText(spannableString);
        view.setMovementMethod(LinkMovementMethod.getInstance());
    }

    public void addIntentLink(final Intent intent, final TextView view) {
        addIntentLink(intent, view, 0, view.getText().toString().length());
    }
}

IntentSpan:把點擊事件轉發出去

最後這個類別繼承 ClickableSpan,本身不做任何事,只把 onClick() 轉發給外部注入的 listener:

package com.example.android.note.util;

import android.text.style.ClickableSpan;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * @author eidolon
 */
public class IntentSpan extends ClickableSpan {

    private final OnClickListener listener;

    public IntentSpan(View.OnClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        listener.onClick(view);
    }
}

設計上的巧思

整個做法的關鍵在於「把 Intent 的啟動責任交給 Activity,把點擊偵測交給 Span」。addIntentLink 提供了兩個 overload:全長版本讓你可以只讓文字中的某一段變成可點擊,無參數版本則是整串文字都可點。而開頭那段 getSpans() 檢查,是為了避免同一段文字被重複套用 span 造成事件衝突——這是實際使用時很容易忽略的防護。

Copyright © Linux教程網 All Rights Reserved