How to create TextView that will act as a link

后端 未结 4 744
长情又很酷
长情又很酷 2020-12-30 17:41

I have a Textview with location:

eg. \"Mountain View, CA\"

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

4条回答
  •  悲哀的现实
    2020-12-30 18:32

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

提交回复
热议问题