2014-04-28 21 views
0

我知道如何給一個超鏈接的單詞,但現在我需要給一個段內的單詞超鏈接?如何給超鏈接到段落內的一個詞在android

例如我的一段話:

Lorem Ipsum is simply dummy text of the printing and "typesetting industry". Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 

在這裏,我想超鏈接添加到「排版產業」這個詞 是,可能嗎?如果是這樣,請幫助我獲得解決方案。

回答

2

我可以做到這一點使用下面的代碼,

TextView textView = (TextView)findViewById(R.id.text); 
     textView.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."); 

     MatchFilter mMatchFilter = new MatchFilter() { 
      public final boolean acceptMatch(CharSequence s, int start, int end) { 
       return true; 
      } 
     }; 
     TransformFilter mTransformFilter = new TransformFilter() { 
      public final String transformUrl(final Matcher match, String url) { 
       return "www.google.com"; 
      } 
     }; 

     Pattern mPattern = Pattern.compile("typesetting industry"); 
     String scheme = "http://"; 
     Linkify.addLinks(textView, mPattern, scheme, mMatchFilter, mTransformFilter); 
+0

爲什麼你返回www.google.com? – Madhu

+0

非常感謝它的工作。 – Madhu

+0

我很高興它適合你。 –

0

親愛Brindha使用這個特定的文字,您可以設置網頁或電子郵件或電話或地圖或全部按您的要求。

android:autoLink =「web | email | phone | map | all」

+0

感謝您的回答,因爲我需要單獨給出段落中不同詞的鏈接,所以第一個答案對我有幫助。 – Madhu