如何创建的TextView,将作为一个链接作为一个、链接、TextView

2023-09-05 09:55:18 作者:坟场蹦迪

我有一个的TextView 与位置:

如。 加州山景城

我想实现的是创建这个文本表现得像一个链接 - 颜色,下划线,可聚焦性等

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.

推荐答案

这样的事情应该工作。

Something like this should work.

  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);