Android可点击的span点击激活所有TextView而不仅仅是spannable文本。
创始人
2024-10-08 16:34:20
0

要实现Android可点击的span点击激活所有TextView,可以通过自定义ClickableSpan来实现。以下是一个示例代码:

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ClickableSpanExample {

    private Context context;

    public ClickableSpanExample(Context context) {
        this.context = context;
    }

    public void activateTextView(TextView textView) {
        String text = textView.getText().toString();

        SpannableString spannableString = new SpannableString(text);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                // 点击span时执行的操作
                Toast.makeText(context, "Span Clicked", Toast.LENGTH_SHORT).show();
            }
        };

        spannableString.setSpan(clickableSpan, 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        textView.setText(spannableString);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setHighlightColor(Color.TRANSPARENT);
    }
}

使用示例:

TextView textView = findViewById(R.id.textView);
ClickableSpanExample clickableSpanExample = new ClickableSpanExample(this);
clickableSpanExample.activateTextView(textView);

在上面的示例中,我们使用自定义的ClickableSpan来处理span的点击事件。在点击span时,会显示一个Toast消息。要激活TextView的点击事件,我们需要将TextView的movement method设置为LinkMovementMethod,并将highlight color设置为透明。这样点击TextView上的任何文本或span时都会触发ClickableSpan的onClick方法。

相关内容

热门资讯

重大通报!wepoker怎么挂... 重大通报!wepoker怎么挂飞机,wepoker有没有机器人,2025新版技巧(有挂透明)1、玩家...
总算了解!htx矩阵wepok... 1、总算了解!htx矩阵wepoker辅助,epoker透视底牌,必胜教程(有挂辅助);详细教程。2...
发现玩家!hhpoker有没有... 发现玩家!hhpoker有没有作弊挂,agpoker辅助,大神讲解(有挂神器);支持2-10人实时对...
发现玩家!扑克之星辅助,pok... 发现玩家!扑克之星辅助,pokemmo手机脚本,AI教程(有挂辅助);建议优先通过pokemmo手机...
重大来袭!德州局脚本,wpk辅... 重大来袭!德州局脚本,wpk辅助,辅助教程(有挂技巧);1.德州局脚本 ai辅助创建新账号,点击进入...
玩家必备攻略!hhpoker怎... 玩家必备攻略!hhpoker怎么开透视,pokerworld破解版下载,新版2025教程(有挂技巧)...
实测发现!xpoker怎么作弊... 实测发现!xpoker怎么作弊,wepoker数据分析工具,详细教程(有挂方法);原来确实真的有挂(...
推荐攻略!aapoker怎么设... 推荐攻略!aapoker怎么设置提高好牌几率,wepoker有人用过吗,德州教程(有挂教程)1、很好...
研究成果!hhpoker透视脚... 这是一款非常优秀的hhpoker透视脚本视频 ia辅助检测软件,能够让你了解到hhpoker透视脚本...
今日百科!德州局HHpoker... 1、今日百科!德州局HHpoker透视脚本,wepoker透视脚本网页,必备教程(有挂方法);详细教...