如何创建将充当链接的 TextView [英] How to create TextView that will act as a link

查看:20
本文介绍了如何创建将充当链接的 TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Textview 位置:

例如.加利福尼亚州山景城"

eg. "Mountain View, CA"

我想要实现的是创建此文本以充当链接 - 颜色、下划线、可聚焦性等.

What I want to achieve is to create this text to act like a Link - color,underline, focusability etc.

此链接不需要指向任何地方 - 周围的视图已附加 onClick 侦听器,可触发谷歌地图意图.

This link doesn't need to direct anywhere - surrounding view has attached onClick listener that fires google maps intent.

推荐答案

这样的事情应该可行.

  TextView location = (TextView) findViewById(R.id.location);
  location.setMovementMethod(LinkMovementMethod.getInstance());
  Spannable spans = (Spannable) location.getText();
  ClickableSpan clickSpan = new ClickableSpan() {

     @Override
     public void onClick(View widget)
     {
        //put whatever you like here, below is an example
        AlertDialog.Builder builder = new Builder(MainActivity.this);
        builder.setTitle("Location clicked");
        AlertDialog dialog = builder.create();            
        dialog.show();
     }
  };
  spans.setSpan(clickSpan, 0, spans.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

这篇关于如何创建将充当链接的 TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆